concat不在mysql inclause工作

时间:2015-05-13 07:49:33

标签: mysql concat in-clause

请帮助我,我尝试获取类别列表,如下所示,其中类别ID在inclause中传递,它返回两行。

mysql> select c_id,c_name from category where c_id in (870,854);
+------+---------------+
| c_id | c_name        |
+------+---------------+
|  854 | Telugu        |
|  870 | Telugu Events |
+------+---------------+

虽然相同的类别id被连接并作为参数传递给inclause,但它只返回一行两行。

mysql> select c_id,c_name from category where c_id in (select concat(870,',',854) as c_id);
+------+---------------+
| c_id | c_name        |
+------+---------------+
|  870 | Telugu Events |
+------+---------------+

请澄清我。

感谢。

2 个答案:

答案 0 :(得分:1)

不确定为什么要在concat中使用in-clause,当您拥有一些动态数据时需要这样做,为此您需要使用prepare

这是怎么做的

set @c_id := concat(870,',',854);
set @qry = concat("select * from category where c_id in (",@c_id,")");

prepare stmt from @qry;
execute stmt;

<强> DEMO

答案 1 :(得分:0)

我找到了解决方案,

SELECT c_id, c_name 
FROM category 
WHERE FIND_IN_SET(c_id, (SELECT CONCAT(870,',',854) AS c_id))

感谢。