简体   繁体   English

对mysql表进行排序,删除它并创建一个已排序的新表

[英]Sort a mysql table, drop it and create new one sorted

I have a table with the following structure: 我有一个具有以下结构的表:

country, city, population 国家,城市,人口

The table is alphabetically sorted by city, I want to sort by population desc and create a new table that is sorted this way. 该表按字母顺序按城市排序,我想按人口desc排序并创建一个按此方式排序的新表。

What would be the query to be used in this case? 在这种情况下要使用的查询是什么?

您可以使用单个查询获得所需的结果:

Create table new_table select * from table order by population desc;

I don't understand why you would need to do this when you can just: 我不明白为什么你需要这样做才能做到:

select country, city, population from table order by population desc;

To answer you question: 回答你的问题:

create table new_table like table;
insert into new_table select * from table order by population desc;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法删除MySQL表(如果存在)并创建新表注意:#1051未知表 - can't drop MySQL table if exist and create new one Note: #1051 Unknown table MYSQL:有没有一种方法可以在触发时创建新表或删除现有表? - MYSQL: Is there a way to CREATE a new table or DROP the existing table on trigger? 如果不创建新表,则重命名mysql表(如果存在) - Rename mysql table if exist if not create a new one mysql优化表或创建一个新表 - mysql optimize table or create a new one SQL(mysql)排序表并获取其在排序表中的位置的行? - SQL (mysql) sort table and get row with its position in the sorted table? 如何在插入新行后使用DESC排序的主键创建MySQL表并保持这种方式? - How to Create a MySQL Table with the Primary Key sorted by DESC and keep it that way after INSERTing new rows? MySQL列限制。 使用一个表还是创建一个新表? - Mysql columns limit. Using one table or create a new one? MySQL数据库结构将值推入一个字段或创建新表 - MySQL Database Structure Push Values to One Field or Create New Table 组合 MySQL 表中的 2 列以创建具有特定字符串的新列 - Combining 2 columns in MySQL table to create a new one with a specific string 在MySQL中删除Create table vs Truncate table - Drop Create table vs Truncate table in Mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM