我正在尝试将一个列值交换为表中的 2 条记录。 我有一个主键:'recnr'
Recnr File 1 img001.jpg 2 img002.jpg 5 img005.jpg 6 img006.jpg
我希望文件 img005.jpg 向上移动一位:
Recnr File 1 img001.jpg 2 img005.jpg 5 img002.jpg 6 img006.jpg
如何做到这一点?
最佳答案
您可以添加一个名为 weight 的整数字段,并将其与 ORDER BY 子句一起使用。
这将是您的原始数据集:
Recnr | File | weight
1 | img001.jpg | 0
2 | img002.jpg | 0
5 | img005.jpg | 0
6 | img006.jpg | 0
为了提高一排,你可以减轻它的重量
Recnr | File | weight
1 | img001.jpg | 0
2 | img002.jpg | -10
5 | img005.jpg | 20
6 | img006.jpg | 0
然后您 SELECT * ORDER BY weight ASC 使最轻的记录出现在顶部。 结果会是
Recnr | File | weight
2 | img002.jpg | -10
1 | img001.jpg | 0
6 | img006.jpg | 0
5 | img005.jpg | 20
关于sql - 如何交换表中 2 条记录的列值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5045955/