Giới thiệu
Dùng information_schema.tables kết hợp GROUP_CONCAT để liệt kê tất cả tên bảng trong một database thành một chuỗi duy nhất.
Truy vấn
SELECT GROUP_CONCAT(table_name) FROM information_schema.tables WHERE table_schema = 'web';
Kết quả (ví dụ)
+----------------------------------------------------------------------------------------------------------------------------------+ | GROUP_CONCAT(table_name) | +----------------------------------------------------------------------------------------------------------------------------------+ | demo_table725,demotabe619,demotabe620,demotable211,demotable212,...,demotable288 | +----------------------------------------------------------------------------------------------------------------------------------+
Lưu ý quan trọng
GROUP_CONCATmặc định giới hạn 1024 ký tự. Danh sách bảng nhiều sẽ bị cắt.- Tăng giới hạn:
SET SESSION group_concat_max_len = 1000000;(trước khi chạy SELECT). - Thêm
ORDER BY table_nameđể có thứ tự ổn định. - Lọc
table_type = 'BASE TABLE'để loại VIEW.
Truy vấn hoàn chỉnh
SET SESSION group_concat_max_len = 1000000; SELECT GROUP_CONCAT(table_name ORDER BY table_name) AS all_tables FROM information_schema.tables WHERE table_schema = 'web' AND table_type = 'BASE TABLE';
Cách khác: SHOW TABLES
SHOW TABLES FROM `web`;
Trả về result set nhiều dòng (mỗi bảng 1 dòng), phù hợp cho script xử lý.