如何重新排序表中的列值? [英] How to reorder column values in a table?

查看:37
本文介绍了如何重新排序表中的列值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从表中删除一些行时,删除后看起来像下表:

When I delete some rows from a table it looks like the following table after removing:

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

问题是 ID 没有正确排序.
我需要像这样重新排序:

The problem is the ID isn't ordered properly.
I need it to be reordered like this:

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

是否有命令可以像所示那样对列重新排序?
我正在使用 sqlite

Is there a command to reorder the column like shown?
I'm using sqlite

推荐答案

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

I think this type of query should do that:

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

这篇关于如何重新排序表中的列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆