开发者

using OR on mysql JOINs

开发者 https://www.devze.com 2023-04-04 02:33 出处:网络
Is there a way to specify a mysql query that does something like this SELECT * FROM a LEFT JOIN b ON (a.xyz = b.xyz OR a.xyr = b.xyr);

Is there a way to specify a mysql query that does something like this

SELECT * FROM a LEFT JOIN b ON (a.xyz = b.xyz OR a.xyr = b.xyr);

and therefore returns rows when any of the LEFT JOIN criteria on the ON statement matches up...

if so what would be the syntax of such qu开发者_开发知识库eries?


Split the select statement into two parts and use a UNION.

SELECT * FROM a LEFT JOIN b on a.xyz = b.xyz
UNION
SELECT * FROM a LEFT JOIN b on a.xyr = b.xyr

If the UNION in MySQL is like the UNION in SQL Server, it should produce a set without duplicate rows. Otherwise, you may need to use a DISTINCT.


According the the docs the ON clause is a conditional expression. So yes, you can use such an expression. In fact, exactly what you have should work.

However, depending on your data and join type, it could produce some unintuitive results.

0

精彩评论

暂无评论...
验证码 换一张
取 消