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

MySQL: Cú pháp đúng cho NOT LIKE (SHOW TABLES)

Cú pháp NOT LIKE trong SHOW TABLES dùng để lọc tên bảng không khớp pattern. Phải dùng backtick cho identifier Tables_in_ten_db.

Cú pháp

SHOW TABLES WHERE `Tables_in_ten_database` NOT LIKE 'pattern%';

Ví dụ minh họa

Database sample có 9 bảng:

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

Lọc bảng KHÔNG bắt đầu bằng "insert"

mysql> SHOW TABLES WHERE `Tables_in_sample` NOT LIKE 'insert%';

Kết quả (6 bảng không liên quan đến "insert"):

+--------------------+
| Tables_in_sample   |
+--------------------+
| blobsizedemo       |
| mytable            |
| newlinedemo        |
| notequaloperator   |
| sumofeverydistinct |
| yourtable          |
+--------------------+

Lưu ý:

  • Tables_in_sample là tên cột ảo của kết quả SHOW TABLES (format: Tables_in_)
  • Bắt buộc dùng backtick vì tên cột chứa underscore và không phải identifier chuẩn
  • Pattern 'insert%' khớp: insert_prevent, insertrecord_selecttable, insertrecordprevent

Cách thay thế (mạnh hơn):

SELECT table_name FROM information_schema.tables 
WHERE table_schema = 'sample' AND table_name NOT LIKE 'insert%';