方法1:
select nb_info.*,nb_corp.id,nb_corp.flag,nb_corp.qymc,nb_corp.tm,nb_corp.[user],nb_corp.trust_score from nb_Info join nb_corp on nb_corp.flag=1 and nb_corp.type=2 and nb_corp.id=nb_info.gsid and nb_info.flag=1 order by nb_corp.idasc,nb_corp.trust_score desc 方法2:select nb_info.*,nb_corp.id,nb_corp.flag,nb_corp.qymc,nb_corp.tm,nb_corp.[user],nb_corp.trust_score from nb_Info,nb_corp where nb_corp.flag=1 and nb_corp.type=2 and nb_corp.id=nb_info.gsid and nb_info.flag=1 order by nb_corp.idasc,nb_corp.trust_score desc 请问这两个方法结果有区别吗?对于join on 和where的使用我一直分不清,请帮忙解惑下,谢谢!!
select nb_info.*,nb_corp.id,nb_corp.flag,nb_corp.qymc,nb_corp.tm,nb_corp.[user],nb_corp.trust_score from nb_Info join nb_corp on nb_corp.flag=1 and nb_corp.type=2 and nb_corp.id=nb_info.gsid and nb_info.flag=1 order by nb_corp.idasc,nb_corp.trust_score desc 方法2:select nb_info.*,nb_corp.id,nb_corp.flag,nb_corp.qymc,nb_corp.tm,nb_corp.[user],nb_corp.trust_score from nb_Info,nb_corp where nb_corp.flag=1 and nb_corp.type=2 and nb_corp.id=nb_info.gsid and nb_info.flag=1 order by nb_corp.idasc,nb_corp.trust_score desc 请问这两个方法结果有区别吗?对于join on 和where的使用我一直分不清,请帮忙解惑下,谢谢!!
假设有表@tb1和表@tb2declare @tb1 table(id int,field1 int) insert @tb1 select 1 , 11 insert @tb1 select 2 , 21 insert @tb1 select 3 , 31 declare @tb2 table(id int,field2 int) insert @tb2 select 1 , 111 insert @tb2 select 2 , 221 insert @tb2 select 4 , 441tb1id field1----------- -----------1 112 213 31Tb2id field2----------- -----------1 1112 2214 441
我们再来看看下面两条sql语句有什么区别:语句一:select * from @tb1 a left outer join @tb2 b on a.id=b.id where a.id=2 语句二:select * from @tb1 a left outer join @tb2 b on a.id=b.id and a.id=2先看看结果是不是跟你想象的一样:id field1 id field2----------- ----------- ----------- -----------2 21 2 221 id field1 id field2----------- ----------- ----------- -----------1 11 NULL NULL2 21 2 2213 31 NULL NULL如果不明白为什么是这个结果的话,回头看一下Outer join的概念其中的一句话:而外部联接会返回 FROM 子句中提到的至少一个表或视图中的所有行,只要这些行符合任何 WHERE 或 HAVING 搜索条件。再来看语句一,left join虽然会保留左边表中的所有记录,不管它符不符合on中的条件,但是左边表@tb1中只有一条记录符合where中的条件id=2,所以仅显示一条记录,而语句二中,尽管id=1和id=3不符合on中的条件id=2,但是它们仍然会显示出来,当做跟@tb2表中不存在符合的记录处理。本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zjb211434/archive/2009/08/02/4401846.aspx