我正在创建简单的图库,每个图库都可以是混合图像/视频。在图库列表中,我想显示每个图库的图像/视频数量。但是,我只收到一个结果行,并且真的不知道为什么。
样本库表:
id title
=====================
1 Simple gallery
2 Test gallery
简单的gallery_items表:
id gallery_id type title
================================
1 1 image title1
2 1 image title2
3 1 video title3
4 1 image title4
5 2 image title5
6 2 image title6
查询我使用的是:
select galleries.*, count(distinct A.id) as IMG, count(distinct B.id) as VID
from galleries
left join gallery_items A on (galleries.id=A.gallery_id and A.type='image')
left join gallery_items B on (galleries.id=B.gallery_id and B.type='video')
这产生了预期的结果:
id title IMG VID
================================
1 Simple gallery 3 1
但问题是只显示一行。无法显示所有行......有什么想法吗?
答案 0 :(得分:0)
是的,Salmon是对的......只需添加"由galleries.id"解决问题:)
select galleries.*, count(distinct A.id) as IMG, count(distinct B.id) as VID
from galleries
left join gallery_items A on (galleries.id=A.gallery_id and A.type='image')
left join gallery_items B on (galleries.id=B.gallery_id and B.type='video')
group by galleries.id