SQL: connecting tables that have no relation
If i have two table开发者_StackOverflow中文版s that have nothing in common
I want to do a select as follows:
column from table1, column from table 2
How do I do it?
Try a UNION ALL
SELECT columnA FROM table1 UNION ALL SELECT columnB from table2
This seems to work in SQL Server assuming you want the Cartesian Product
SELECT A.Column, B.Column
FROM TableA A, TableB B
Meaning, you want the cartesian product of the two tables?
Look at doing a CROSS JOIN.
If you're not concerned with the columns appears beside each other AND the data types are compatible, you can use UNION.
SELECT columnA FROM table1
UNION
SELECT columnB FROM table2
SELECT table1.column, table2.column
FROM table1, table2
gives you the cartesian product. Not really sure if that's what you need though?
Well, you can't really unless both tables have have zero or one row then you can use Jacob G's answer.
Otherwise, 3 rows in one and 2 rows in another will give the Cartesian product = 6 rows.
If you don't want the cross/Cartesian product then you're asking for a jagged recordset which is not possible. How should rows from one table relate to a row in the other table?
If they are truly unrelated, it's 2 separate calls...
If both tables have a single row this isn't a big issue:
SELECT ( SELECT COLUMN1 FROM TABLE1 ) AS COL1, ( SELECT COLUMN1 FROM TABLE2 ) AS COL2;
See the other answers for the cartesian product and this link which is weary of them:
http://www.rampant-books.com/t_hpsdba_77_cartesian_join_operations.htm
精彩评论