mysql 表名为变量

    直接使用变量不行  需要通过创建存储过程中使用的动态 sql 。。

mysql> set @a = concat('select * from ',unhex('73696E5F75736572'));
Query OK, 0 rows affected (0.00 sec)

mysql> select @a;
+------------------------+
| @a                     |
+------------------------+
| select * from sin_user |
+------------------------+
1 row in set (0.00 sec)

mysql> PREPARE stmt from @a;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> EXECUTE stmt;
+-----+----------+------------------------------------+----------+-------+---------------------------------------------------------------------------------+--------------------+-----------------------------+
| uid | username | password                           | nickname | role  | photo                                                                           | email              | description                 |
+-----+----------+------------------------------------+----------+-------+---------------------------------------------------------------------------------+--------------------+-----------------------------+
|   1 | sincoder |b620111121044922.png | [email protected] | C/C++ 程序设计爱好者        |
+-----+----------+------------------------------------+----------+-------+---------------------------------------------------------------------------------+--------------------+-----------------------------+
1 row in set (0.00 sec)

mysql> DEALLOCATE PREPARE stmt;
Query OK, 0 rows affected (0.00 sec)

你可能感兴趣的:(mysql 表名为变量)