MySQL: Match against dynamic values

334 views Asked by At

I have a list of values in a table column that I need to match against table names, preferably just using an SQL statement.

If the values were static, I suppose the SELECT statement would be something like this:

SELECT table_name FROM information_schema.TABLES WHERE 
match(table_name) against('124512' +'36326' +'23636' IN BOOLEAN MODE)

However, I need to match against dynamic values coming from a SELECT statement:

SELECT tableid FROM databaseName.tableOverviewTableName 
WHERE template = 'templateName')

The tableid above is contained in the table_name for the tables that I want.

Is this possible to achieve with an SQL statement?

1

There are 1 answers

1
TJ- On

You can do this via Prepared statement (not directly via a query)

SET @tq = (SELECT tableid FROM databaseName.tableOverviewTableName WHERE template = 'templateName'));
SET @stmq = CONCAT('SELECT * FROM ', @tq);
Prepare stmt FROM @stmq;
Execute stmt;
DEALLOCATE PREPARE stmt;