join and PK where and

来源:互联网 发布:淘宝网收藏删除的宝贝 编辑:程序博客网 时间:2024/06/05 10:44

ONE .left join and

left join and也是以A表的记录为基础的,A可以看成左表,B可以看成右表,left join and是以左表为准的。换句话说,左表(A)的记录将会全部表示出来,而右表(B)只会显示符合搜索条件的记录(例子中为: A.ID = B.ID)。B表记录不足的地方均为NULL,加上and条件后,A表记录也将全部被表示出来,而B表只会将符合条件的记录显示出来,B表记录中不符合条件的地方均显示为null。

select a.id,a.name,b.id,b.subject,b.score from tmp_lxq_1 a
left join tmp_lxq_2 b
on a.id=b.id
and b.score>=80;

ID NAME ID SUBJECT SCORE
1 张三 1 语文 80
2 李四 2 数学 90
3 王五

TWO .left join where

left join where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。

select a.id,a.name,b.id,b.subject,b.score from tmp_lxq_1 a
left join tmp_lxq_2 b
on a.id=b.id
where b.score>=80;

ID NAME ID SUBJECT SCORE
1 张三 1 语文 80
2 李四 2 数学 90

0 0