MySQL创建字符串变量

问题描述 投票:0回答:1

有没有办法为MySQL变量使用字符串组合?

我知道如果这只是一串数字,这不是问题,但我可以用这种方式使用SET吗?

mysql> select @SQL_BIND_DOCS;
+-----------------------------------------------------------------------+
| @SQL_BIND_DOCS                                                       |
+-----------------------------------------------------------------------+
| '1','2','3','4','5' |
+-----------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select distinct doc from  documents doc where doc in (@SQL_BIND_DOCS);
Empty set (0.01 sec)

mysql> select distinct doc from documents doc where doc in ('1','2','3','4','5');
+---------------------------------+
| doc                             |
+---------------------------------+
| AA                              |
| BB                              |
| CC                              |
| DD                              |
| EE                              |
+---------------------------------+
5 rows in set (0.01 sec)

您可以在此处看到数据没有问题 - 任何提供的帮助都将受到赞赏。

mysql variables
1个回答
1
投票

你可以使用FIND_IN_SET

select distinct doc from  documents doc where FIND_IN_SET(doc, @SQL_BIND_DOCS);

在您的情况下,使用IN不起作用,因为变量是cast to 1

参考

FIND_IN_SET() vs IN()

difference between where_in and find_in_set

Why find_in_set works but IN clause

© www.soinside.com 2019 - 2024. All rights reserved.