mysql left join on 条件中加OR
时间: 2023-11-03 21:02:33 浏览: 302
可以在 MySQL 的 LEFT JOIN ON 条件中使用 OR 来连接多个条件,例如:
SELECT *
FROM table1
LEFT JOIN table2
ON table1.id = table2.id OR table1.name = table2.name
这个查询会将 table1 和 table2 中 id 相同或者 name 相同的记录连接起来。注意,在使用 OR 连接条件时,需要使用括号来明确条件的优先级,例如:
SELECT *
FROM table1
LEFT JOIN table2
ON (table1.id = table2.id OR table1.name = table2.name) AND table2.status = 'active'
这个查询会将 table1 和 table2 中 id 相同或者 name 相同,并且 table2 的 status 为 'active' 的记录连接起来。
相关推荐















