MySQL 显示表按表名排序?

mysqlmysqli database

您可以使用 ORDER BY 子句对 INFORMATION_SCHEMA.TABLES 中的 table_name 属性进行排序。分别使用 ASC 或 DESC 按升序或降序排序。语法如下 −

SELECT table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE' AND table_schema='yourDatabaseName'
ORDER BY table_name DESC;

使用名称为 sample 的数据库并有一些表。首先,我们将显示所有表,然后我们将应用对表名进行排序。显示所有表的查询如下 −

mysql> show tables;

以下是输出 −

+--------------------------+
| Tables_in_sample         |
+--------------------------+
| blobsizedemo             |
| insert_prevent           |
| insertrecord_selecttable |
| insertrecordprevent      |
| mytable                  |
| newlinedemo              |
| notequaloperator         |
| sumofeverydistinct       |
| yourtable                |
+--------------------------+
9 rows in set (0.00 sec)

这是按表名排序的查询。现在,让我们使用 ORDER BY 子句按降序显示所有表 −

mysql> SELECT table_name
   -> FROM information_schema.tables
   -> WHERE table_type = 'BASE TABLE' AND table_schema='sample'
   -> ORDER BY table_name DESC;

以下是输出 −

+--------------------------+
| TABLE_NAME               |
+--------------------------+
| yourtable                |
| sumofeverydistinct       |
| notequaloperator         |
| newlinedemo              |
| mytable                  |
| insertrecordprevent      |
| insertrecord_selecttable |
| insert_prevent           |
| blobsizedemo             |
+--------------------------+
9 rows in set (0.00 sec)

相关文章