我可以在mysql命令行中声明一个变量吗?

时间:2016-11-10 00:48:20

标签: mysql

我想说我想做$table = 'datatables_demo';所以我可以做select * from $table?我/我怎么做?

LOG BELOW

mysql> select * from datatables_demo;                                           +----+------------+-----------+-------------+--------+------------+--------+
| id | first_name | last_name | position    | office | start_date | salary |
+----+------------+-----------+-------------+--------+------------+--------+
|  1 | Tiger      | Nixon     | Accountant  | Tokyo  | 2016-11-08 | 320800 |
|  2 | Garrett    | Winters   | Accountant2 | Tokyo  | 2016-11-08 | 170750 |
|  3 | Ashton     | Cox       | Accountant3 | Tokyo  | 2016-11-08 |  86000 |
|  4 | Cedric     | Kelly     | Accountant4 | Tokyo  | 2016-11-08 | 433060 |
|  5 | Tiger5     | Nixon     | Accountant  | Tokyo  | 2016-11-08 | 320800 |
+----+------------+-----------+-------------+--------+------------+--------+
5 rows in set (0.00 sec)

mysql> $table = 'datatables_demo';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$table = 'datatables_demo'' at line 1
mysql> table = 'datatables_demo';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table = 'datatables_demo'' at line 1
mysql> var table varchar2(20)
    -> ;

1 个答案:

答案 0 :(得分:1)

您应该使用SELECT来实现动态SQL语句。

给你举个例子

INSERT