I’m trying to use a user-defined variable to stand-in for a table name so that I can just use mysql and not require any other languages.
This example is way simplistic – the real script uses the table name a LOT and it’d be easier for end-users to be able to just change the variable rather than search/replace.
SET @tablename = 'mytable';
SELECT * FROM @tablename;
This won’t work, but shows the end result I need.
Use prepared statements:
If your actual query is far more complicated, you could wrap the building of the query in some stored function:
Even better: do everything in a stored procedure: