我想回答以下问题。我需要选择在Track列中收听了track 1和track 2的用户。出于某种原因,我完全迷失了如何做到这一点。
答案 0 :(得分:3)
您可以计算每个用户收听的相关曲目的数量:
SELECT user_id
FROM mytable
WHERE track IN ('track_1', 'track_2')
GROUP BY user_id
HAVING COUNT(DISTINCT track) = 2
答案 1 :(得分:1)
以下是其中一种方式:
SELECT user_id from TABLE_NAME
WHERE track = 'track_1' AND
user_id IN
(SELECT user_id from TABLE_NAME where track = 'track_2');
答案 2 :(得分:0)
如果只有两个,你也可以使用GROUP BY和HAVING:
SELECT user_id
FROM t
GROUP BY user_id
HAVING COUNT(DISTINCT track) > 1
答案 3 :(得分:0)
可能比需要存在子查询的地方更简单。
Select userID
from yourtable
where Track in ('Track 1','Track 2')
group by userid
having count(1) = 2
答案 4 :(得分:0)
这有效吗?
SELECT UserId
FROM YourTable
WHERE Track in ('track_1','track_2')
group by UserId
Having count(distinct Track) > 1;