SQL 语句中 left join 后用 on 还是 where,区别大了!_A left B join on an

 

前天写SQL时本想通过 A left B join on and 后面的条件来使查出的两条记录变成一条,奈何发现还是有两条。后来发现 join on and 不会过滤结果记录条数,只会根据and后的条件是否显示 B表的记录,A表的记录一定会显示。不管and 后面的是=1还是=1,都显示出A表中所有的记录,并关联显示B中对应A表中id为1的记录或者B表中id为1的记录。运行sql : select * from student s left join class c on s.classId= order by s.idSQL 语句中 left join 后用 on 还是 where,区别大了!_条件记录_02运行sql : select * from student s left join class c on s.classId= and ="张三" order by s.idSQL 语句中 left join 后用 on 还是 where,区别大了!_A left B join on an_03运行sql : select * from student s left join class c on s.classId= and ="三年级三班" order by s.idSQL 语句中 left join 后用 on 还是 where,区别大了!_A left B join on an_04下面的内容为转载数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。在使用left jion时,on和where条件的区别如下:1、 on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。2、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。假设有两张表:SQL 语句中 left join 后用 on 还是 where,区别大了!_SQL_05两条SQL:1、select * form tab1 left join tab2 on (tab1.size = tab2.size) where =’AAA’2、select * form tab1 left join tab2 on (tab1.size = tab2.size and =’AAA’)第一条SQL的过程:SQL 语句中 left join 后用 on 还是 where,区别大了!_SQL_06第二条SQL的过程:SQL 语句中 left join 后用 on 还是 where,区别大了!_A left B join on an_07其实以上结果的关键原因就是left join,right join,full join的特殊性,不管on上的条件是否为真都会返回left或right表中的记录,full则具有left和right的特性的并集。而inner jion没这个特殊性,则条件放在on中和where中,返回的结果集是相同的。

-END-

 

SQL 语句中 left join 后用 on 还是 where,区别大了!_A left B join on an_08

SQL 语句中 left join 后用 on 还是 where,区别大了!_A left B join on an_09