SQL中使用JOIN时,过滤条件的位置
sql语句
(SELECT * FROM t1 WHERE {subquery_where_condition}) a
JOIN
(SELECT * FROM t2 WHERE {subquery_where_condition}) b
ON {on_condition}
WHERE {where_condition};
计算顺序
- 子查询中的where子句,即
- JOIN子句中的关联条件,即
- JOIN结果集中的where子句,即
过滤条件位置
- inner join
左右表的过滤条件不受限制 - left join
左表过滤条件放在{subquery_where_condition}或{where_condition}中,右表过滤条件放在{subquery_where_condition}或{on_condition}中 - right join
右表的过滤条件放在{subquery_where_condition}或{where_condition}中,左表过滤条件放在{subquery_where_condition}或 - full outer join
过滤条件只能放在{subquery_where_condition}中
推荐做法
将每个表的单表过滤条件单独写成子查询。
本文来自博客园,作者:jsqup,转载请注明原文链接:https://www.cnblogs.com/jsqup/p/16963186.html

浙公网安备 33010602011771号