简体   繁体   English

MySQL:如何从一列返回所有唯一值,同时匹配另一列中的值

[英]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.

相关问题 在MySQL中,如何计算一列中某个值范围的出现次数,而不是另一列中所有唯一值的出现次数? - How can I count the number of occurrences of a range of values in one column, against all unique values in another, in MySQL? SQL-从一列返回所有唯一的值对,在另一列中返回每个值 - SQL - Return all unique pairs of values from one column, for each value in another column 如何使用另一个表中的匹配值替换/更新列中每个字符串的所有实例? - How do I replace/update all instances of every string in a column with matching values from another table? 如何将一列值从一个表插入到另一个不匹配的模式? - How do I insert a column of values from one table to another, non-matching schemas? 如何从一列中获取唯一值并将其与另一列中的值分组? - How do I get unique values from a column and group them with values from another column? MYSQL仅在该表的列值与另一表的列值匹配的情况下,才如何从该表中选择数据? - MYSQL How do I select data from one table only where column values from that table match the column values of another table? Mysql-如何将一列中的所有值与另一列中的所有值组合? - Mysql- How to combine all values from one columns with all values from another column? 我如何从 MySQL 表中的 select 行按一列分组,另一列具有所需值 - How do I select rows from a MySQL table grouped by one column with required values in another 如何创建MYSQL查询以获取特定结果(从一栏中计算所有唯一值) - How to create MYSQL query to get specific results (count all unique values from one column) 如何为 MySQL 在 group by 中连接列的唯一值? - How do I concat a column's unique values in a group by for MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM