SQL DB2 INNER JOIN OR 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8770948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DB2 INNER JOIN OR Statement
提问by em3ricasforsale
I am writing a query for one of my applications that I support, and I am looking for what will cost less in trying to get a DB2 version of an OR statement in the join. Since you can't use "OR" inside on a
我正在为我支持的应用程序之一编写查询,并且我正在寻找在尝试获取连接中 OR 语句的 DB2 版本时成本更低的方法。由于您不能在内部使用“OR”
Select *
FROM
TABLE A INNER JOIN TABLE B
ON
A.USERNAME = B.NAME
OR
A.USERNAME = B.USERNAME
I have been looking at trying UNION or UNION ALL, but I am worried that the match up may take longer to return.
我一直在考虑尝试 UNION 或 UNION ALL,但我担心比赛可能需要更长时间才能恢复。
回答by Andriy M
While I join others in confirming that OR
can easily be used in join conditions, like in any other conditions (and so your query is perfectly fine), you could still do without OR
in this particular case.
虽然我和其他人一起确认OR
可以很容易地在连接条件中使用,就像在任何其他条件中一样(因此您的查询非常好),但OR
在这种特殊情况下您仍然可以不用。
Conditions like column = value1 OR column = value2
, where the same column is tested against a number of various fixed values, could be transformed into column IN (value1, value2)
. The latter should work no worse than the former and arguably looks clearer in the end.
诸如column = value1 OR column = value2
针对多个不同固定值测试同一列的条件可以转换为column IN (value1, value2)
. 后者应该不会比前者差,并且最终看起来更清晰。
So:
所以:
SELECT *
FROM
TABLE A
INNER JOIN
TABLE B
ON
A.USERNAME IN (B.NAME, B.USERNAME)
回答by ron tornambe
You can revert to using the WHERE clause syntax for joining. If the DB2 optimizer is clever (and I suspect it is), it should perform equally as well as JOIN:
您可以恢复使用 WHERE 子句语法进行连接。如果 DB2 优化器很聪明(我怀疑它很聪明),它的性能应该与 JOIN 一样好:
SELECT * FROM TABLE1 A, TABLE2 B
WHERE A.USERNAME = B.NAME OR A.USERNAME=B.USERNAME
回答by Kumawat
SELECT GROUP_REDUCTION.TOUR, FROM_DATE, TO_DATE, DISCOUNT, GROUP_SIZE, REDUCTION
FROM SEASON_DISCOUNT
RIGHT JOIN GROUP_REDUCTION ON SEASON_DISCOUNT.TOUR = GROUP_REDUCTION.TOUR
ORDER BY GROUP_REDUCTION.TOUR, FROM_DATE;