[英]MySQL: How do I return all unique values from one column while matching values in another column
I have a MySQL DB storing song metadata. 我有一个存储歌曲元数据的MySQL DB。 Two of the existing columns in Table Music are Rating and Title.
桌音乐中现有的两列是“评分”和“标题”。 The DB is over 9 GB large.
该数据库超过9 GB。 I would like to find all unique song titles where the rating is below a value on a 0-10 scale, say 3. (Unique here meaning I only need to see a song title once.)
我想找到所有等级低于0-10的数值(例如3)的唯一歌曲名称。(此处唯一表示只需要查看一次歌曲名称。)
Once I get the resulting list of song titles, I will delete those low-rated songs form the DB. 一旦获得了歌曲标题的结果列表,我将从数据库中删除那些低收视率的歌曲。 However, I have another table in the DB called Albums and I never want to delete any songs in any Album listed there.
但是,数据库中还有另一个表叫做专辑,我从不希望删除那里列出的任何专辑中的任何歌曲。
What I have tried so far is: 到目前为止,我尝试过的是:
SELECT DISTINCT Title FROM Music WHERE
Title NOT IN (SELECT Title FROM Music WHERE Rating >= 3)
AND Song NOT IN (SELECT Song FROM Albums)
ORDER BY Title desc
This query might work; 该查询可能有效; I don't know because it has been running for hours.
我不知道,因为它已经运行了几个小时。 So I also need help in finding a faster way to get the results.
因此,我还需要帮助,以找到更快的方法来获得结果。
Get songs from MUSIC with rating below 3 then do a left join with ALBUM on key: song. 从MUSIC获得评级低于3的歌曲,然后与ALBUM在key:歌曲上进行左连接。 But also put a filter where song in MUSIC is not found in album.
还要放置一个过滤器,使专辑中找不到MUSIC中的歌曲。
SELECT DISTINCT T.TITLE
FROM MUSIC T
LEFT JOIN ALBUM A ON T.SONG = A.SONG
WHERE T.RATING < 3
AND A.SONG IS NULL
ORDER BY T.TITLE DESC;
see demo here; 在这里查看演示;
http://sqlfiddle.com/#!9/65c2455/1 http://sqlfiddle.com/#!9/65c2455/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.