SHOW TABLES statement with multiple LIKE values
Asked Answered
E

6

40
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 ?

Enclave answered 10/4, 2011 at 3:39 Comment(1)
dev.mysql.com/doc/refman/5.0/en/tables-table.htmlAlcaeus
L
74

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%';
Loreanloredana answered 10/4, 2011 at 3:51 Comment(1)
You need to define the db name in the FROM clause - this query will return tables from any database served by the MySQL instance.Alcaeus
C
17

You can just use a normal SQL WHERE statement to do it.

SHOW TABLES WHERE Tables_in_tianyan LIKE '%cms%'
Composure answered 10/4, 2011 at 3:46 Comment(1)
I want to filter by like 'cms' or like 'role', and it seems WHERE Tables_in_tianyan is unnecessary here.Enclave
C
7
show tables from mydb 
where 
  Tables_in_mydb like '%statistics%' 
  or Tables_in_mydb like '%device%';
Chadburn answered 10/4, 2011 at 3:53 Comment(0)
T
3

You take table list using the below code

select TABLE_NAME from information_schema.TABLES where TABLE_SCHEMA = 'database_name' 
Teresiateresina answered 6/1, 2017 at 5:27 Comment(0)
H
1

this will help

SELECT 
TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME
 LIKE 'cms%';
Histamine answered 10/8, 2022 at 10:55 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Karnak
M
0
SHOW TABLES WHERE Tables_in_<yourdbname> REGEXP '^(regex-pattern1|regex-pattern2|...)$';
Montez answered 14/9, 2023 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.