1

I have been trying to write a query for a view but I can't seem to get it... I have two tables that I need to join... but what I need is that for every ID in table 1 I get all records on table 2. For Example:

 Table 1       Table 2
  Jhon          Car
  Bill          House
                Jet
  

I would like to get:

  Table 3
  Jhon   Car
  Jhon   House
  Jhon   Jet
  Bill   Car
  Bill   House
  Bill   Jet

P.S. Data on both tables can vary. P.S.S. Table 1 is actually a Left Outer Join between two other tables where the first table contains the indexes and the second contains the field used to create relation to Table 2.

4 Answers 4

6

You need a CROSS JOIN for this (AKA Cartesian Product).

SELECT t1.col, t2.col
FROM Table1 t1 cross join Table2 t2
Sign up to request clarification or add additional context in comments.

Comments

3

Try this

select * from table1, table2

or use a CROSS JOIN if database supports it

3 Comments

SQL Server does support it so I think it's best practice to use the explicit syntax so it is clear that it is intentional!
If you post code, XML or data samples, please highlight those lines in the text editor and click on the "code samples" button ( { } ) on the editor toolbar to nicely format and syntax highlight it!
This is the "old-style" JOIN syntax - which is both deprecated (you should use explicit joins, e.g. INNER JOIN; OUTER JOIN, CROSS JOIN, as needed) and also less clear and less obvious in my opinion. I would avoid this syntax if ever possible
2
SELECT  *
FROM    table1
CROSS JOIN
        table2

Comments

2
select columns you want to get
from Table1 Cross Join Table2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.