在这种情况下,我需要你的帮助。我有记录表:
+-----------+--------+----------------+---------+------------+
| record_id | record | number_repeats | counter | sort_order |
+-----------+--------+----------------+---------+------------+
| 1 | Value3 | 2 | 0 | 3 |
| 2 | Value2 | 1 | 0 | 2 |
| 3 | Value1 | 3 | 0 | 1 |
| 4 | Value4 | 1 | 0 | 4 |
+-----------+--------+----------------+---------+------------+
我有这个简单的查询:
SELECT * FROM records ORDER BY sort_order
排序后,我需要选择 Value1 并更新列计数器中的值,以便在该表显示之后:
+-----------+--------+----------------+---------+------------+
| record_id | record | number_repeats | counter | sort_order |
+-----------+--------+----------------+---------+------------+
| 3 | Value1 | 3 | 1 | 1 |
| 2 | Value2 | 1 | 0 | 2 |
| 1 | Value3 | 2 | 0 | 3 |
| 4 | Value4 | 1 | 0 | 4 |
+-----------+--------+----------------+---------+------------+
列计数器中的下一个查询号码将 2 ,依此类推。当列计数器中的数字等于 number_repeats 列中的数字时,我需要选择 Value2 ,依此类推。
最好的方法是什么?提前谢谢!
答案 0 :(得分:0)
未测试:
Update my_table set counter = counter +1 where counter < repeats order by sort_order limit 1;