Is there any way in MySQL to put the name of the database into a variable? For example, when I have a database called \'db1\', can I do something like this:
With considerable effort, yes.
SET @db = 'db1'; SET @q = CONCAT('SELECT * FROM ', @db, '.mycol'); PREPARE stmt FROM @q; EXECUTE stmt; DEALLOCATE PREPARE stmt;