【转】left join的总结
EG1:通俗的讲A left join B 的连接的记录数与A表的记录数同A right join B 的连接的记录数与B表的记录数同 A left join B 等价B right join Atable A:Field_K, Field_A1 a3 b4 ctable B:Field_K, Field_B1 x2 y4 zselect a.Field_K, a.Field_A, b.Field_K, b.Field_Bfrom a left join b on a.Field_K=b.Field_KField_K Field_A Field_K Field_B ---------- ---------- ---------- ---------- 1 a 1 x 3 b NULL NULL4 c 4 z select a.Field_K, a.Field_A, b.Field_K, b.Field_Bfrom a right join b on a.Field_K=b.Field_KField_K Field_A Field_K Field_B ---------- ---------- ---------- ---------- 1 a 1 x NULL NULL 2 y 4 c 4 z 这样的。 table1 table2 id,sex1 id sex2 a 1 a 4 b 0select id,sex1,sex2 from table1 left join table2 on table1.id=table2.id 则, id sex1 sex2 a 1 4 b 0 null也就是说left join 则连接左边表中所有记录都会出现,如果根据连接条件在table2中找不到相关记录,
则显示为null。right join 则显示右边表中的全部记录。inner join 则只有符合条件的记录才会出现在结果集中。
EG2∶有两表a和b,前两字段完全相同:(id int,name varchar(10)...)id name ----------- ---------- 1 a 2 b 3 c 以下的查询语句,你知道它的运行结果吗?:1.select * from a left join b on a.id=b.id where a.id=12.select * from a left join b on a.id=b.id and a.id=13.select * from a left join b on a.id=b.id and b.id=14.select * from a left join b on a.id=1结果:id name id name ----------- ----------- ----------- ----------- 1 10 1 &nb