简体   繁体   中英

How do i get all table names who match a specific regex in mysql 8.0?

I'm trying to retrieve all tables with specific name format, for performing union among those tables. I'm using mysql Ver 8.0.13 , and i wrote this following query for retrieving the relevant tables:

show tables LIKE REGEX '^table_.+_class$';

I couldn't figure out the correct syntax for this query :/

Afterwards i'm planning to union all those tables .

I would like to avoid writing this code since it doesn't scale nicely:

SELECT * FROM table_french_class
UNION
SELECT * FROM table_history_class
UNION
SELECT * FROM table_pingpong_class
UNION
SELECT * FROM table_math_class
UNION
SELECT * FROM table_literature_class

Can someone suggest me how to handle this issue?

Thank you

You could use INFORMATION_SCHEMA catalog:

SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME REGEXP '^table_.+_class$';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM