Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

MySQL: Sắp xếp bảng theo tên bảng (SHOW TABLES ORDER BY)

Bạn có thể sắp xếp thuộc tính table_name từ INFORMATION_SCHEMA.TABLES bằng mệnh đề ORDER BY. Sắp xếp tăng dần (ASC) hoặc giảm dần (DESC).

Cú pháp

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

Ví dụ minh họa

Sử dụng database sample có sẵn một số bảng. Trước tiên hiển thị tất cả bảng:

mysql> SHOW TABLES;

Kết quả (chưa sắp xếp):

+--------------------------+
| Tables_in_sample         |
+--------------------------+
| blobsizedemo             |
| insert_prevent           |
| insertrecord_selecttable |
| insertrecordprevent      |
| mytable                  |
| newlinedemo              |
| notequaloperator         |
| sumofeverydistinct       |
| yourtable                |
+--------------------------+

Truy vấn sắp xếp theo tên bảng giảm dần

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

Kết quả đã sắp xếp:

+--------------------------+
| TABLE_NAME               |
+--------------------------+
| yourtable                |
| sumofeverydistinct       |
| notequaloperator         |
| newlinedemo              |
| mytable                  |
| insertrecordprevent      |
| insertrecord_selecttable |
| insert_prevent           |
| blobsizedemo             |
+--------------------------+

Mẹo: Thay DESC bằng ASC để sắp xếp tăng dần (A-Z).