两张表,album(id,name,description),photo(id,albumId,name,description)
photo.albumId是引用的alum.id。现在要查的是每个album的所有信息以及它的photo数目。应该是很简单的,但是我回想了很久,没有想出来,请大家帮帮忙!
SELECT t0.*, ISNULL(t1.Count, 0) FROM album AS t0
LEFT JOIN ( SELECT albumId, Count(*) AS Count FROM photo
GROUP BY albumId
) AS t1 ON t1.albumId = t0.Id
select album.*,count(albumId) as photonum from album
left join photo on album.id=photo.albumId
group by album.id,album.name,album.description
楼上两位大哥回答得都对啊.但是不是count(*)是要把NULL也包括进去啊.