[MSSQL] 巧秒运用On来进行联合查询

On在查询中已经不少见了,不过用On可有秒处了
不用On与用where进行查询时
select a.ID,a.G_Title,Count(b.ID) from [TD_GuideTag] as a left outer join [TD_Engine] as b on (a.ID = b.E_TagID) where (b.E_Url like '%我们%' or b.E_Name like '%我们%' or b.E_IndexUrl like'%我们%'Group by a.ID,a.G_Title,a.G_OrderNumeric,a.G_UpDateTime order by a.G_OrderNumeric asc,a.G_UpDateTime desc
发现当a表中有数据,于b表中没有数据时,查询的结果为空
select a.ID,a.G_Title,Count(b.ID) from [TD_GuideTag] as a left outer join [TD_Engine] as b on (a.ID = b.E_TagID and (b.E_Url like '%我们%' or b.E_Name like '%我们%' or b.E_IndexUrl like'%我们%') ) Group by a.ID,a.G_Title,a.G_OrderNumeric,a.G_UpDateTime order by a.G_OrderNumeric asc,a.G_UpDateTime desc
但当把条件放在ON里面时,查询结果会列出,只是Count(b.ID)=0
很多时候我们想就算没有数据也要显示时,用这个最好不过了。
posted @ 2009-09-25 11:42  竹子小毅  阅读(282)  评论(0编辑  收藏  举报