[英]FULL OUTER JOIN or UNION?
confused how to achieve this maybe with FULL OUTER or a UNION. 困惑如何实现这一点可能与FULL OUTER或UNION。 I want to join results in such a way that 我想以这样的方式加入结果
Table1 Table2
--------------- -----------------
ID Name Salary ID Fruits
--------------- -----------------
1 John 1000 1 Apples
1 Henry 4000 1 Mangoes
1 Smith 1000 1 Tomatoes
Result should be 结果应该是
ResultTable
------------------------
ID Name Salary Fruits
-----------------------
1 John 1000 Apples
1 John 1000 Mangoes
1 John 1000 Tomatoes
1 Henry 4000 Apples
1 Henry 4000 Mangoes
1 Henry 4000 Tomatoes
1 Smith 1000 Apples
1 Smith 1000 Mangoes
1 Smith 1000 Tomatoes
You need a cartesian product join or Cross Join .. 您需要笛卡尔产品加入或交叉加入..
SELECT
*
FROM
table1, table2
or 要么
SELECT
*
FROM
table1 CROSS JOIN table2
(reference: http://publib.boulder.ibm.com/iseries/v5r2/ic2924/index.htm?info/sqlp/rbafymstcrojo.htm ) (参考: http : //publib.boulder.ibm.com/iseries/v5r2/ic2924/index.htm?info / sqlp / rbafymstcrojo.htm )
如果db2有它,请使用cross join
You might want to rethink your DB naming standards - naming columns as "ID" but then allowing duplicates isn't a great idea IMHO. 您可能想要重新考虑您的数据库命名标准 - 将列命名为“ID”,但允许重复不是一个好主意恕我直言。
Because of the duplication, it isn't quite clear what is needed, but assuming that ID is a red herring, I think a CROSS JOIN is the correct approach in your scenario 由于重复,不太清楚需要什么,但假设ID是红鲱鱼,我认为CROSS JOIN是你方案中的正确方法
select t1.ID, t1.Name, t1.Salary, t2.Fruit
from Table1 t1, Table2 t2
OR 要么
select t1.ID, t1.Name, t1.Salary, t2.Fruit
from Table1 t1 CROSS JOIN Table2 t2
As the other answers have said, if you want all the rows in Table1 for all the rows in Table2, then a cross join (ie. a cartesian join) is the answer. 正如其他答案所说,如果你想要Table2中的所有行表2中的所有行,那么交叉连接(即笛卡尔连接)就是答案。
On the other hand, in the scenario above, an inner join on ID would also return the same resultset: 另一方面,在上面的场景中,ID上的内部联接也将返回相同的结果集:
select t1.ID, t1.Name, t1.Salary, t2.Fruit
from Table1 t1 join Table2 t2 on t1.id = t2.id
Then again, if this query relates to the same problem as some of your Crystal questions, you might find this resultset more useful: 然后,如果此查询与某些Crystal问题存在相同的问题,您可能会发现此结果集更有用:
ResultTable ResultTable
------------------------
ID Name Salary Fruits
-----------------------
1 John 1000
1 Henry 4000
1 Smith 1000
1 Apples
1 Mangoes
1 Tomatoes
which can be obtained with this query: 可以使用此查询获得:
select ID, Name, Salary, '' Fruit from Table1 union all
select ID, '' Name, NULL Salary, Fruit from Table2
Use a CROSS JOIN
: 使用CROSS JOIN
:
SELECT T1.Name, T1.Salary, T2.Fruit
FROM Table1 AS T1
CROSS JOIN Table2 AS T2;
Avoid using infixed notation: 避免使用固定符号:
SELECT T1.Name, T1.Salary, T2.Fruit
FROM Table1 AS T1, Table2 AS T2;
I'm not quite sure why but folk definitely don't like the infixed notation, especially when you add a WHERE
clause. 我不太清楚为什么但是民间肯定不喜欢加密的符号,特别是当你添加一个WHERE
子句时。 They will tell you that CROSS JOIN
is SQL-92, which is indeed correct but then infixed notation is SQL-92 as well. 他们会告诉你CROSS JOIN
是SQL-92,这确实是正确的,但是后面的输入符号也是SQL-92。
Oh, and some folk call the infixed notation without a WHERE
clause a Cartesian Product, which is indeed correct but then a CROSS JOIN
is a Cartesian Product as well. 哦,有些人把没有WHERE
子句的加密符号称为笛卡儿积,这确实是正确的,但是CROSS JOIN
也是笛卡尔积。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.