我有两张桌子:
+---------+-----------------+----------+----------+----------+---------+
| TrackId | URI | ArtistID | Title | AlbumID | BitRate |
+---------+-----------------+----------+----------+----------+---------+
| 1 | /home/music/... | 234 | atune | 8958223 | 192 |
| 2 | /music/uri1/... | 427 | goodsong | 222 | 192 |
| 3 | /music/uri2/... | 427 | goodsong | 222 | 128 |
| 4 | /music/uri3/... | 427 | goodsong | 222 | 160 |
| 5 | /home/music/... | 427 | goodsong | 333 | 128 |
| 6 | /home/music/... | 522 | another | 3458859 | 128 |
+---------+-----------------+----------+----------+----------+---------+
和
+----------+------------+
| AlbumID | AlbumTitle |
+----------+------------+
| 8958223 | titleA |
| 222 | titleB |
| 333 | titleC |
| 3458859 | titleD |
+----------+------------+
简单地说,我想要的是这样的:
+---------+-----------------+----------+----------+----------+------------+---------+
| TrackId | URI | ArtistID | Title | AlbumId | AlbumTitle | BitRate |
+---------+-----------------+----------+----------+----------+------------+---------+
| 3 | /music/uri2/... | 427 | goodsong | 222 | titleB | 128 |
| 4 | /music/uri3/... | 427 | goodsong | 222 | titleB | 160 |
+---------+-----------------+----------+----------+----------+------------+---------+
这是尝试删除具有以下内容的重复项:
- 同名
- 相同的艺术家 ID
- 不同的轨道ID
- 非最高比特率重复
- 专辑表中的相同专辑名称
同时不返回具有最高比特率副本的条目
我在这里问了一个非常相似的问题:从单行中选择重复项?
解决方案是:
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)
...但这次我似乎无法专心处理另一张桌子。