简体   繁体   English

DB2 INNER JOIN OR语句

[英]DB2 INNER JOIN OR Statement

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. 我正在为我支持的一个应用程序编写查询,并且正在寻找尝试在联接中获取OR2语句的DB2版本的成本更低的东西。 Since you can't use "OR" inside on a 由于您不能在

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,但我担心比赛可能需要更长的时间才能返回。

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) . 诸如column = value1 OR column = value2类的条件(可以针对多个固定值测试同一列)可以转换为column IN (value1, value2) The latter should work no worse than the former and arguably looks clearer in the end. 后者的工作原理应不比前者差,并且最终看起来更清晰。

So: 所以:

SELECT *
FROM
  TABLE A
INNER JOIN
  TABLE B
ON 
  A.USERNAME IN (B.NAME, B.USERNAME)

You can revert to using the WHERE clause syntax for joining. 您可以恢复为使用WHERE子句语法进行联接。 If the DB2 optimizer is clever (and I suspect it is), it should perform equally as well as JOIN: 如果DB2优化器很聪明(我怀疑是这样),它的性能应与JOIN一样好:

SELECT * FROM TABLE1 A, TABLE2 B
WHERE A.USERNAME = B.NAME OR A.USERNAME=B.USERNAME
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;

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

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