MySQL - 在单独的列中出现重复项时更新最大值
需要对 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 语句:
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 |