SELECT FROM (with function)

I’m curious how to hack it. Could anyone with more experience help? Is Recursive Common Table Expressions correct way?

SET @lang = 'en';
SELECT * FROM CONCAT(@lang, '_mytable')

You can use dynamic table or column names with prepared statements:

SET @lang = 'en';
SET @sql = CONCAT('SELECT * FROM ', @lang, '_mytable');
PREPARE statement FROM @sql;
EXECUTE statement;
DEALLOCATE PREPARE statement;

However I suggest to not overcomplicate things. Instead of having a table per language you could have a single table with the language code in a key or index.

1 Like