mysql 选择在哪里

标签 mysql sql

我有一个完美运行的子查询:

select GROUP_CONCAT(zone SEPARATOR ', ') from typeofru where id in (5,7) /*This works good*/

我遇到的问题是 5 和 7 一起在 varchar 列 ('5,7') 上,无法使其工作

select GROUP_CONCAT(zone SEPARATOR ', ') from typeofru where id in ('5,7') /*This is not working  */

如何将 ('5,7') 转换为值类型以使其正常工作

我需要使用的最终查询是这样的,但似乎不起作用:

select name, (select GROUP_CONCAT(zone SEPARATOR ', ') from typeofru where id in (typeofru_ids)) from deviceru where device_id=5

最佳答案

使用FIND_IN_SET()

WHERE FIND_IN_SET(ID, '5,7') > 0

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (“,”) character.

作为一个教训,不要存储用逗号分隔的值。

如果逗号前后有空格,需要先替换空格,

WHERE FIND_IN_SET(ID, REPLACE(' 5 , 7', ' ','')) > 0

关于mysql 选择在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16626507/

相关文章:

sql - 具有最小和最大条件的交叉连接

mysql - 事件记录 (Rails) MySQL 安全连接

php - Codeigniter 2.2,MySQL - 导入 csv 后获取所有 id

mysql AS 子句

mysql集群管理器: Unable to connect to mysql client instance mysql cluster 7. 5.5

sql - Postgres 外键 'on update' 和 'on delete' 选项如何工作?

mysql - 根据描述对列进行排序

mysql 查找距离今天超过 1 周的日期

mysql - 如何从sql中经过的天数中查找年份和月份

sql - 如何使用 BETWEEN 运算符比较两个 SELECT 子查询?