การใช้ LIKE กับ SHOW TABLES
คำสั่ง SHOW TABLES รองรับ LIKE สำหรับกรองชื่อตารางด้วย Pattern (Wildcard: % = หลายตัวอักษร, _ = ตัวอักษรเดี่ยว)
รูปแบบคำสั่ง
SHOW TABLES LIKE 'your_pattern';
ตัวอย่างการใช้งาน
ฐานข้อมูล test ต้องการหาตารางที่ชื่อลงท้ายด้วย _d (Pattern: %d_)
SHOW TABLES LIKE '%d_';
ผลลัพธ์
| Tables_in_test (%d_) |
|---|
| differenceinseconds |
| lasthourrecords |
| skiplasttenrecords |
Pattern ที่ใช้บ่อย
| Pattern | ความหมาย | ตัวอย่าง Match |
|---|---|---|
user% | ขึ้นต้นด้วย user | user, user_profile, user_logs |
%log | ลงท้ายด้วย log | error_log, access_log, audit_log |
%temp% | มี temp อยู่กลาง | temp_data, user_temp_table |
t_ _ | ขึ้นต้น t_ ตามด้วย 2 ตัวอักษร | t_01, t_ab, t_xy |
_[0-9]% | ตัวที่ 2 เป็นตัวเลข | a1_table, x9_data |
วิธีทางเลือก: INFORMATION_SCHEMA (ขั้นสูงกว่า)
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'test'
AND TABLE_NAME LIKE '%d\_%' ESCAPE '\';
ข้อดีของ INFORMATION_SCHEMA
- 🔍 รองรับ
REGEXPสำหรับ Pattern ซับซ้อน:WHERE TABLE_NAME REGEXP '^(user|admin|log)' - 📊 ดูข้อมูลเพิ่ม: Rows, Size, Engine, Create Time, Comment
- 🔗 JOIN กับ COLUMNS, STATISTICS, TABLE_CONSTRAINTS ได้
- 💾 ใช้ใน Subquery/CTE ได้
ตัวอย่าง REGEXP
-- ตารางขึ้นต้น user_ หรือ admin_ หรือ log_
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'test'
AND TABLE_NAME REGEXP '^(user_|admin_|log_)';