如何重新排序表中的列值?

时间:2015-04-14 10:01:17

标签: sql sqlite

当我从表中删除某些行时,删除后会显示如下表:

+-----+--------+----------+
| ID  |  Name  | Address  |
+-----+--------+----------+
| 1   | name1  | address1 |
| 2   | name2  | address2 |
| 8   | name8  | address8 |
| 9   | name9  | address9 |
+-----+--------+----------+

问题是ID未正确订购 我需要像这样重新排序:

+----+---------+----------+
| ID |  Name   | Address  |
+----+---------+----------+
| 1   | name1  | address1 |
| 2   | name2  | address2 |
| 3   | name8  | address8 |
| 4   | name9  | address9 |
+-----+--------+----------+

是否有命令重新排序列,如图所示?
我正在使用sqlite

1 个答案:

答案 0 :(得分:0)

我认为这种类型的查询应该这样做:

UPDATE yourTable
SET ID = (SELECT COUNT(*) FROM yourTable ti WHERE ti.ID <= yourTable.ID)