variable database name

后端 未结 3 1116
余生分开走
余生分开走 2020-12-09 03:38

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:

         


        
3条回答
  •  遥遥无期
    2020-12-09 04:22

    With considerable effort, yes.

    SET @db = 'db1';
    SET @q = CONCAT('SELECT * FROM ', @db, '.mycol');
    PREPARE stmt FROM @q;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

提交回复
热议问题