链表查询,求解,争....
两张表
MyFavorites
id int
userid int
typeid int
ImageType
id int
userid int
imgtype varchar(50)
实现效果
id imgType num
1 test1 1
2 test2 0
说明:MyFavorites表中只有一条test1 数据
ImageType表中有两条数据(test1,和test2)
MyFavorites和ImageType 表中有一条以imgType和typeid 相合的数据
我的写法
select i.id,imgType,COUNT(TypeId)num
from dbo.ImageType as i inner join MyFavorites as m
on i.UserId=m.UserId
where i.UserId=00123
group by imgType,TypeId,i.id
结果
id imgType num
1 test1 1
2 test2 1
这个结果不是我想要的,因为test2 在MyFavorites 表中并没有相配对的数据,这里应该显示是0
求解...
------解决方案--------------------
SQL code
select m.id,m.imgType,isnull(COUNT(i.TypeId),0) as num
from dbo.ImageType as i left join MyFavorites as m
on i.UserId=m.UserId
where i.UserId=00123
group by imgType,TypeId,i.id