MYSQL:如何“重新排序”一张桌子
我有一个如下所示的表,
| id | name | color |
------+--------+---------
| 1 | pear | green |
| 2 | apple | red |
| 3 | banana | yellow |
| 4 | grape | purple |
我想使用“名称”列按字母顺序重新排序,并使用此新顺序重置 id(自动增量),最终得到以下
| id | name | color |
------+--------+---------
| 1 | apple | red |
| 2 | banana | yellow |
| 3 | grape | purple |
| 4 | pear | green |
问题:我怎样才能用 MYSQL 做这个吗?
I have a table like the following,
| id | name | color |
------+--------+---------
| 1 | pear | green |
| 2 | apple | red |
| 3 | banana | yellow |
| 4 | grape | purple |
I'd like to reorder alphabetically using the "name" column and reset the id (autoincrement) with this new order to end up with the following
| id | name | color |
------+--------+---------
| 1 | apple | red |
| 2 | banana | yellow |
| 3 | grape | purple |
| 4 | pear | green |
QUESTION: how can I do this with MYSQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
重置自动增量的最简洁方法是创建另一个表。
MySQL 提供了诸如 CREATE TABLE LIKE 和 RENAME TABLE 等有用的命令。
The cleanest way to reset the auto increment is to create another table.
MySQL provides commands such as
CREATE TABLE LIKE
andRENAME TABLE
that are useful.我可以问你为什么要这样做吗?
如果任何人修改任何名称值或插入新行,则会扰乱您的排序方案。尝试在表中其他地方(名称列)中已经可用的 PK 排序中存储一些含义似乎是多余的,因此是一个坏主意。
更好的解决方案是,当您在应用程序中使用数据时,不必担心 ID 列的值,而只需对名称列进行排序。
PS:对于非答复类型的回复深表歉意。通常我会假设你有一个很好的理由,只是给出一个直接解决你想要做的事情的答案,但我从你的其他问题中注意到你仍处于数据库设计的早期学习阶段,所以我想提供帮助为您指明正确的方向,而不是帮助您进一步迈向不明智的方法。
Can I ask why you would want to do this?
If anyone modifies any of the name values or inserts new rows it will mess up your ordering scheme. Trying to store some meaning in the ordering of the PK that is already available elsewhere in the table (the name column) seems redundant and consequently a bad idea.
A much better solution is not to worry about the value of the ID column and just sort on the name column when you use the data in your app.
PS: Sorry for the non-answer type response. Normally I'd assume you had a good reason and just give an answer that directly addresses what you are trying to do, but I noticed from your other questions that you are still in the early learning stages about database design, so I wanted to help point you in the right direction instead of helping further your progress towards an ill-advised approach.
您可以SELECT INTO旧表中的新表,根据需要将您的选择排序。新表中有一个自增ID。如果需要,删除旧表并重命名新表。
You can SELECT INTO a new table from the old table, ordering your select into as desired. Have an auto-increment ID in the new table. If needed, drop the old table and rename the new table.
为什么不在查询末尾添加“ORDER BY name ASC”?我的猜测是您出于某种原因需要 ID。
Why not adding "ORDER BY name ASC" at the end of your query? My guess would be that you need the ID for some reason.
如果您有一个带有自动增量主键(例如名为“id”)的表,并且该键不被其他表依赖,则解决此问题的方法是这样的。
alter table order by column_x, column_y;
我使用 phpmyadmin 这样做了几次,非常成功并且速度很快。
对以主键作为索引的表进行重新排序是不可能的。这就是为什么你需要先将其删除。
如果您需要保留“id”列,但需要根据其他列重新排序,则需要省略主键和主键。 id列的索引状态&采取。然后您需要添加一个新列作为主键/索引。
If you have a table with an autoincrement primary key (named 'id' for example), and the key is not being depended on by other tables, the way to go about this is this.
alter table order by column_x, column_y;
I did this a few times, with success and quite fast, using phpmyadmin.
Reordering a table that has a primary key as an index, is impossible. That's why u need to remove it first.
If u need to keep the 'id' column, but need to re-sort, based on other columns, u need to omit the primary key & index status of the id column & re-sort. Then you need to add a new column as primary key / index.
可以保存到临时表,然后截断,然后截断水果表并插入,但这可能是一个蹩脚的解决方案。
could save to a temp table then truncate then truncate the fruit table and insert, but it's probably a crappy solutions.