在mysql shell中设置一个带有元组值的变量

时间:2013-06-30 06:18:17

标签: mysql variables

我在mysql shell中设置了这样的变量

mysql> set @nilesh := 330;
Query OK, 0 rows affected (0.00 sec)

工作正常,但当我试图设置一个元组时,

mysql> set @nilesh := (330, 221);
ERROR 1241 (21000): Operand should contain 1 column(s)

我收到错误,请建议我如何使用mysql的元组初始化变量。

1 个答案:

答案 0 :(得分:3)

你不能这样做,并且不清楚为什么你想要它。但是你可以做类似dynamic-sql的方式

mysql> set @ids = '1, 2';
Query OK, 0 rows affected (0.00 sec)

mysql> set @sql = concat('select * from table1 where id in (', @ids, ')');
Query OK, 0 rows affected (0.00 sec)

mysql> select @sql;
+-----------------------------------------+
| @sql                                    |
+-----------------------------------------+
| select * from table1 where id in (1, 2) |
+-----------------------------------------+
1 row in set (0.00 sec)

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

mysql> execute stmt;
+------+-----------+------+
| id   | col1      | col2 |
+------+-----------+------+
|    1 | value1    |   11 |
|    2 | value2    |   12 |
+------+-----------+------+
2 rows in set (0.00 sec)

mysql> deallocate prepare stmt;
Query OK, 0 rows affected (0.00 sec)