对整个SQLite表进行排序 [英] Sort an entire SQLite table
问题描述
我有一个需要排序的SQLite表.我熟悉ORDER BY
命令,但这不是我要完成的工作.我需要将整个表在数据库中排序.
I have an SQLite table that I need to sort. I am familiar with the ORDER BY
command but this is not what I am trying to accomplish. I need the entire table sorted within the database.
说明:
我的表使用名为rowed
的列,该列设置表的顺序(键?).我需要按另一列称为name
的表格对表格进行排序,然后根据name
的字母顺序重新分配行编号.能做到吗?
My table uses a column called rowed
which sets the order of the table (a key?). I need to sort the table by another column called name
and then re-assign rowid numbers in alphabetical order according to name
. Can this be done?
推荐答案
假设您像这样创建原始表:
Assuming you created your original table like so:
CREATE TABLE my_table (rowid INTEGER PRIMARY KEY, name TEXT, somedata TEXT) ;
您可以创建另一个排序表,如下所示:
You can create another sorted table like so:
CREATE TABLE my_ordered_table (rowid INTEGER PRIMARY KEY, name TEXT, somedata TEXT) ;
INSERT INTO my_ordered_table (name, somedata) SELECT name,somedata FROM my_table
ORDER BY name ;
然后如果您要替换原始表:
And if you then want to replace the original table:
DROP TABLE my_table ;
ALTER TABLE my_ordered_table RENAME TO my_table;
这篇关于对整个SQLite表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!