SHOW TABLES statement with multiple LIKE values

mysql_go picture mysql_go · Apr 10, 2011 · Viewed 78.1k times · Source
mysql> SHOW TABLES like 'cms';
+-------------------------+
| Tables_in_tianyan (cms) |
+-------------------------+
| cms                     |
+-------------------------+
1 row in set (0.00 sec)

Result

mysql> SHOW TABLES like 'cms' or like 'role';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual...

How can I filter by multiple conditions ?

Answer

Rich Adams picture Rich Adams · Apr 10, 2011

You need to use the WHERE clause. As shown in the docs, you can only have a single pattern if you use "SHOW TABLES LIKE ...", but you can use an expression in the WHERE clause if you use "SHOW TABLES WHERE ...". Since you want an expression, you need to use the WHERE clause.

SHOW TABLES
FROM `<yourdbname>`
WHERE 
    `Tables_in_<yourdbname>` LIKE '%cms%'
    OR `Tables_in_<yourdbname>` LIKE '%role%';