我目前正在将一些媒体资产迁移到第三方应用程序 (3PA),我们的内部系统已将资产记录在下面的示例数据中,但是 3PA 要求 SoundRecordingCode 与 ISRC 相关,因此我需要运行批量更新语句针对与我的示例数据集类似的 10000 个条目。
样本数据集:
| Id | SongCode | SoundRecordingCode | ISRC | ContractId |
|----|----------|--------------------|------------|--------------|
| 1 | 100 | 201 | ISRC_001 | 43 |
| 2 | 100 | 202 | ISRC_001 | 43 |
| 3 | 100 | 203 | ISRC_001 | 43 |
| 4 | 100 | 204 | ISRC_001 | 43 |
| 5 | 100 | 205 | ISRC_001 | 43 |
| 6 | 101 | 206 | ISRC_002 | 43 |
| 7 | 101 | 207 | ISRC_002 | 43 |
| 8 | 102 | 208 | ISRC_003 | 43 |
| 9 | 102 | 209 | ISRC_003 | 43 |
| 10 | 102 | 210 | ISRC_003 | 43 |
我正在尝试将 SoundRecordingCode 的值更新为 MAX(SoundRecordingCode),其中 ISRC 重复,因此我希望最终结果为:
| Id | SongCode | SoundRecordingCode | ISRC | ContractId |
|----|----------|--------------------|------------|--------------|
| 1 | 100 | 205 | ISRC_001 | 43 |
| 2 | 100 | 205 | ISRC_001 | 43 |
| 3 | 100 | 205 | ISRC_001 | 43 |
| 4 | 100 | 205 | ISRC_001 | 43 |
| 5 | 100 | 205 | ISRC_001 | 43 |
| 6 | 101 | 207 | ISRC_002 | 43 |
| 7 | 101 | 207 | ISRC_002 | 43 |
| 8 | 102 | 210 | ISRC_003 | 43 |
| 9 | 102 | 210 | ISRC_003 | 43 |
| 10 | 102 | 210 | ISRC_003 | 43 |
我正在使用MySQL
我尝试过以下更新语句:
UPDATE digitalassets t1
JOIN (SELECT Id, MAX(SoundRecordingCode) AS MaxSoundRecordingCode, ISRC
FROM digitalassets
WHERE ContractId = 43
GROUP BY ISRC
HAVING COUNT(ISRC) > 1) t2 USING(Id)
SET t1.SoundRecordingCode = t2.MaxSoundRecordingCode;
但是这仅导致更新一行:
| Id | SongCode | SoundRecordingCode | ISRC | ContractId |
|----|----------|--------------------|------------|--------------|
| 1 | 100 | 205 | ISRC_001 | 43 |
| 2 | 100 | 202 | ISRC_001 | 43 |
| 3 | 100 | 203 | ISRC_001 | 43 |
| 4 | 100 | 204 | ISRC_001 | 43 |
| 5 | 100 | 205 | ISRC_001 | 43 |
| 6 | 101 | 207 | ISRC_002 | 43 |
| 7 | 101 | 207 | ISRC_002 | 43 |
| 8 | 102 | 210 | ISRC_003 | 43 |
| 9 | 102 | 209 | ISRC_003 | 43 |
| 10 | 102 | 210 | ISRC_003 | 43 |
提前非常感谢。
连接列应该与您在子查询中分组的列相同。
UPDATE digitalassets t1
JOIN (SELECT MAX(SoundRecordingCode) AS MaxSoundRecordingCode, ISRC
FROM digitalassets
WHERE ContractId = 43
GROUP BY ISRC
HAVING COUNT(ISRC) > 1) t2 USING(ISRC)
SET t1.SoundRecordingCode = t2.MaxSoundRecordingCode;
如果您启用了 SQL 模式
only_full_group_by
(自 MySQL 5.7 以来一直是默认模式),您的查询将会出现错误。