彩虹制造者
发布于

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           |
浏览 (38)
点赞
收藏
1条评论
Klustron小助手
联接列应与您在子查询中分组的列相同。 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 模式(自 MySQL 5.7 以来一直是默认模式),则查询将出错only_full_group_by
点赞
评论