简体   繁体   English

如果每个用户的mysql选择计数大于3

[英]mysql select count for each user if greater than 3

I have the following mysql table (the real table is actually very huge, about 2 million rows): 我有以下mysql表(实际表实际上非常大,大约200万行):

----------------------------
userId | artistId | trackId
----------------------------
user1  | artist1  | track1
-------|----------|--------
user1  | artist1  | track1
-------|----------|--------
user1  | artist1  | track1
-------|----------|--------
user2  | artist1  | track1
-------|----------|--------
user2  | artist2  | track2
-------|----------|--------
user2  | artist2  | track2
-------|----------|--------
 ....  |   ....   |  ....

What I would like to is: for each user, select artists that users listened more than 3 different tracks of them (ie, 3 tracks of the same artist). 我想要的是:对于每个用户,选择用户收听了3个以上不同曲目的艺术家(即同一位艺术家的3个曲目)。 This is because I need to consider this selection as users preferences on artists, so if eg, a user listened only to 1 or two tracks of an artist, I don't want to consider it as "preferences/likes". 这是因为我需要将此选择视为用户对艺术家的偏好,因此,例如,如果某个用户仅收听了艺术家的一首或两首曲目,那么我就不会将其视为“偏好/喜欢”。 Here is the query that I wrote but I am not sure if this is correct: 这是我写的查询,但是不确定是否正确:

select p.userId, p.artistId, p.trackId 
from lastfm_part2 p 
join 
(select userId, artistId, trackId 
from lastfm_part2 
group by userId, artistId, trackId 
having count(trackId) > 3) as m 
on m.userId = p.userId and m.artistId = p.artistId and p.trackID = m.trackId

PS. PS。 I need to return all the rows, even though they may seem to be duplicates (same user, same track, same artist), but in reality they are related to different time stamps. 我需要返回所有行,即使它们似乎是重复的(相同的用户,相同的曲目,相同的艺术家),但实际上它们与不同的时间戳相关。 I appreciate if someone help me understand if this query is correct. 如果有人帮助我了解此查询是否正确,我将不胜感激。

Thanks 谢谢

I tested it in Oracle so maybe MySQL is a little different but the next query did work for me. 我在Oracle中对其进行了测试,因此MySQL可能有所不同,但下一个查询确实对我有用。

SELECT p.userId, p.artistId, COUNT( DISTINCT p.trackId )
FROM lastfm_part2
group by userId, artistId
having count( DISTINCT p.trackId ) > 3;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM