10:40 2013-08-29
JOIN ON...AND

1 A left join B on A.col1=B.col1 and A.col2=xx
2 A left join B on A.col1=B.col1 where A.col2=xx

前面一种情况A.col2=xx是作为与 B的关联条件,满足on条件的返回B值,否则B为NULL(只影响B是否为NULL)
后面一种情况A.col2=xx是作为where筛选条件,满足where条件的A才能作为左表(影响A的行数)
在left join和right join的时候on条件不会删减“主”表的数据

mysql> select * from t1;
+----+------+
| id | cnum |
+----+------+
|  1 |  100 |
|  2 |  200 |
|  3 |  300 |
|  4 |  400 |
+----+------+
mysql> select * from t2;
+----+--------+-------+
| id | weight | exist |
+----+--------+-------+
|  2 |     22 |     0 |
|  4 |     44 |     1 |
|  5 |     55 |     0 |
|  6 |     66 |     1 |
+----+--------+-------+
mysql> select * from t1 a
left join t2 b
on a.id=b.id
and b.weight!=44
and b.exist=0
where b.id is null;
+----+------+------+--------+-------+
| id | cnum | id   | weight | exist |
+----+------+------+--------+-------+
|  1 |  100 | NULL | NULL   | NULL  |
|  3 |  300 | NULL | NULL   | NULL  |
|  4 |  400 | NULL | NULL   | NULL  |
+----+------+------+--------+-------+
View Code

相关文章:

  • 2021-04-14
  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-18
  • 2021-06-07
  • 2021-04-21
  • 2021-10-24
  • 2021-12-03
  • 2021-10-11
  • 2021-04-12
相关资源
相似解决方案