简体   繁体   English

SQL:两个表的所有行合并在一起

[英]SQL: All rows of two tables merged together

I am trying to combine two different tables in a select statement where all the rows in the first table are matched with all the rows in the second table. 我试图在select语句中合并两个不同的表,其中第一个表中的所有行与第二个表中的所有行匹配。 For example: 例如:

Table1
Table1_ID | FKey_Table2_ID
1           9
2           null

Table2
Table2_ID | Table2_Value
9           Yes
10          No
11          Maybe

Results needed: 需要的结果:

Table1_ID | FKey_Table2_ID | Table2_ID | Table2_Value
1           9                9           Yes
1           null             10          No
1           null             11          Maybe
2           null             9           Yes
2           null             10          No
2           null             11          Maybe

Please note that the first row in Table1 has a key already assigned from Table2. 请注意,表1的第一行具有已从表2分配的键。

This is called a cross join and can be accomplished like this: 这称为交叉连接 ,可以通过以下方式完成:

SELECT Table1_ID, FKey_Table2_ID, Table2_ID, Table2_Value
FROM Table1
CROSS JOIN Table2

Or more simply 或更简单

SELECT Table1_ID, FKey_Table2_ID, Table2_ID, Table2_Value
FROM Table1, Table2
SELECT * FROM Table1
CROSS JOIN
Table2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM