按表名對 MySQL 展示表進行排序?


你可以用 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)

更新時間:30-Jul-2019

1K+ 瀏覽

開啟你的 職業生涯

完成課程並獲得認證

開始學習
廣告