mysql left join 左关联on条件失效问题

 

sql join 失效问题:

1. left join:以左表为基准,根据on条件过滤连接生成临时表,on后面的过滤条件对左表无效

2. right join:以右表为基准,根据on条件过滤连接生成临时表,on后面的过滤条件对右表无效

3. full join:以左表为基准,根据on条件过滤连接生成临时表,on后面的过滤条件对左右表无效

4. inner join:等值连接,根据过滤条件生成临时表。on后面的条件 可以用 where实现

 

举例说明:

sql1 :

1 SELECT *
2 FROM user_info A
3          LEFT JOIN  user_info_detail B ON (A.id = B.user_id) and B.is_deleted = 0
4 WHERE A.type = 2
5   AND A.is_deleted = 0;

 

sql2:

SELECT *
FROM user_info A
         LEFT JOIN  user_info_detail B ON (A.id = B.user_id)
WHERE A.type = 2
  AND A.is_deleted = 0
  and B.is_deleted = 0;

sql1 和sql2的逻辑完全不一样,使用sql2时,当出现B表没有相应的数据时,sql2会查询不出来数据,sql2会把A表的数据给过滤掉,所以最好优先使用sql1。

 

posted @ 2022-11-17 16:39  [浪子回头]  阅读(2010)  评论(0编辑  收藏  举报