考虑以下问题:
SELECT t.recording_id, m.release_id
FROM track t
JOIN medium m ON t.medium_id = m.medium_id
我得到的结果与此类似
recording id release id
----------------------------------
1 25
1 25
1 37
1 76
1 300
1 336
2 37
... ...
我需要输出以下内容
recording id count
---------------------------------------------------
1 5
2 1
换句话说,我需要通过recording_id进行分组,但不计算该recording_id的release_id重复项
在研究了这个板后,我尝试了以下方法,但没有成功:
SELECT t.recording_id, count(t.recording_id)
FROM track t
JOIN medium m ON t.medium_id = m.medium_id
group by t.recording_id, m.release_id
但是,我正在
recording id release id
--------------------------
1 2
1 1
1 1
1 1
1 1
2 1
出了什么问题?
答案 0 :(得分:2)
试试这个,您可以在count函数中使用distinct来为recording_id返回不同的释放ID
SELECT t.recording_id, count(distinct m.release_id) cnt
FROM track t
JOIN medium m ON t.medium_id = m.medium_id
group by t.recording_id