gpt4 book ai didi

php - 插入新行时移动 mysql 行?

转载 作者:行者123 更新时间:2023-11-29 05:16:44 24 4
gpt4 key购买 nike

假设我们有一个类似于前 100 名的表格,我们将在表格中保持这个排名。让我说清楚;

Singer|AlbumName|Rank(unique)
Eminem|xxxxxxxxx|1
Dr.Dre|×××××××××|2
50cent|xxxxxxxxx|3
..................
..................
Usher |xxxxxxxxx|99
Bieber|i'm b*tch|100

我想做的是插入新行时如何更新它们的排名?移动所有其他行的排名?

Singer|AlbumName|Rank(unique)
Eminem|xxxxxxxxx|1
Sdog |xxxxxxxxx|2
Dr.Dre|×××××××××|3
50cent|xxxxxxxxx|4
..................
Usher |xxxxxxxxx|100

实现这个逻辑是什么?最佳做法是什么?

最佳答案

如果以后有人需要,我已经找到了解决方案;

UPDATE `album` SET `rank`= rank + 1 WHERE rank >= $rankOfNewAlbum ORDER BY rank DESC

通过这样做,您开始向后更新行并且没有错误,因为没有匹配等级。

然后我们可以插入新行。

感谢 mario.klump 的贡献和启发。

关于php - 插入新行时移动 mysql 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31739363/

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