1

我有一个名为 'user_permission' 的表,有一个名为 location_ids 的列,数据类型为 varchar(255)。我在这里存储了10,27,36 之类的值。现在我想在IN 比较运算符中使用这个 'location_ids' 。我尝试了以下查询,但没有得到预期的结果。

SELECT (SELECT GROUP_CONCAT( `name` SEPARATOR ',' ) as name FROM location WHERE `remove` = 0 AND id IN(up.location_ids)) AS name FROM user_permission AS up

但是如果我提供 IN(10,27,36) 而不是 IN(up.location_ids) 那么它就可以工作了。

4

2 回答 2

1

使用FIND_IN_SET()函数

尝试这个:

SELECT up.id, GROUP_CONCAT(l.name) AS `name`
FROM user_permission AS up 
LEFT JOIN location l ON FIND_IN_SET(l.id, up.location_ids) AND l.remove = 0
GROUP BY up.id;

或者

SELECT (SELECT GROUP_CONCAT(`name` SEPARATOR ',') AS NAME 
        FROM location 
        WHERE `remove` = 0 AND FIND_IN_SET(id,up.location_ids)
      ) AS NAME 
FROM user_permission AS up;
于 2014-12-02T08:46:20.970 回答
0

看看这是否有效

SELECT (SELECT GROUP_CONCAT( `name` SEPARATOR ',' ) as name FROM location WHERE `remove` = 0 AND id IN(select location_ids from user_permission)) AS name FROM user_permission
于 2014-12-02T08:34:51.507 回答