gpt4 book ai didi

mysql - 使用mysql : How to add unique id for the same data in another colmun

转载 作者:行者123 更新时间:2023-11-29 21:11:54 26 4
gpt4 key购买 nike

如何将唯一 ID 插入 car_id 列,如下表所示?

|car_id|     CarId  |
+------+------------+
| 1 | 1234 |
| 1 | 1234 |
| 1 | 1234 |
| 2 | 5678 |
| 2 | 5678 |
| 2 | 5678 |
| 3 | 9101112 |

最佳答案

使用变量:

   SELECT CarId, 
@seq := IF(@cid = CarId, @seq,
IF(@cid := CarId, @seq + 1, @seq + 1)) AS car_id
FROM mytable
CROSS JOIN (SELECT @seq := 0, @cid := 0) AS vars
ORDER BY CarId

Demo here

关于mysql - 使用mysql : How to add unique id for the same data in another colmun,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36294600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com