这里我有一个名为CoreTracks的表:
+---------+-----------------+----------+----------+----------+---------+
| TrackId | URI | ArtistID | Title | FileSize | BitRate |
+---------+-----------------+----------+----------+----------+---------+
| 1 | /home/music/... | 234 | atune | 8958223 | 192 |
| 2 | /home/music/... | 427 | goodsong | 6954373 | 192 |
| 3 | /home/music/... | 427 | goodsong | 4695698 | 128 |
| 4 | /home/music/... | 427 | goodsong | 5839962 | 160 |
| 5 | /home/music/... | 427 | goodsong | 4695698 | 128 |
| 6 | /home/music/... | 522 | another | 3458859 | 128 |
+---------+-----------------+----------+----------+----------+---------+
我想要检索的是:
+---------+-----------------+----------+----------+----------+---------+
| TrackId | URI | ArtistID | Title | FileSize | BitRate |
+---------+-----------------+----------+----------+----------+---------+
| 3 | /home/music/... | 427 | goodsong | 4695698 | 128 |
| 4 | /home/music/... | 427 | goodsong | 5839962 | 160 |
| 5 | /home/music/... | 427 | goodsong | 4695698 | 128 |
+---------+-----------------+----------+----------+----------+---------+
我正在尝试删除基于具有相同标题,相同艺术家ID和不同轨道ID的重复项,同时不返回具有最高比特率和最高文件大小的条目。
到目前为止我所拥有的是:
SELECT * FROM CoreTracks
WHERE Title = Title AND ArtistID = ArtistID
AND BitRate != (SELECT MAX(BitRate) FROM CoreTracks WHERE Title = Title AND ArtistID = ArtistID)
AND FileSize != (SELECT MAX(FileSize) FROM CoreTracks WHERE Title = Title AND ArtistID = ArtistID);
返回每个曲目。我缺少什么才能使这个查询有效?
答案 0 :(得分:2)
这将得到反转(即跳过重复):
SELECT c1.*
FROM CoreTracks c1
,(SELECT Title, ArtistID, MAX(FileSize) AS maxFileSize, MAX(BitRate) maxBitRate
FROM CoreTracks
GROUP BY Title, ArtistID) c2
WHERE c1.Title = c2.Title
AND c1.ArtistID = c2.ArtistID
AND (c1.FileSize = c2.maxFileSize OR c1.BitRate = c2.maxBitRate)
重复:
SELECT c1.*
FROM CoreTracks c1
,(SELECT Title, ArtistID, MAX(FileSize) AS maxFileSize, MAX(BitRate) maxBitRate
FROM CoreTracks
GROUP BY Title, ArtistID) c2
WHERE c1.Title = c2.Title
AND c1.ArtistID = c2.ArtistID
AND (c1.FileSize != c2.maxFileSize AND c1.BitRate != c2.maxBitRate)
答案 1 :(得分:0)
按标题从Group中选择max(trackId),按BitRate asc排序ArtistID - 然后将其包含在trackId的另一个select中?
答案 2 :(得分:0)
SELECT A.* FROM CoreTracks A, CoreTracks B
WHERE A.Title = B.Title AND A.ArtistID = B.ArtistID AND A.trackId != B.trackId
HAVING A.BitRate != MAX(A.BitRate) AND A.FileSize != MAX(A.FileSize);
尚未测试但应该有效。
答案 3 :(得分:0)
尝试自联接而不是普通查询。 我没试过。