Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 组_Concat的使用条件_Mysql_Sql_Database - Fatal编程技术网

Mysql 组_Concat的使用条件

Mysql 组_Concat的使用条件,mysql,sql,database,Mysql,Sql,Database,我想查找组_concat仅包含“a”的所有ID。 这是我的简化表 ╔════╦══════════════╦ ║ id ║ group_concat ║ ╠════╬══════════════╬ ║ 1 ║ a,b,b ║ ║ 2 ║ a ║ ║ 3 ║ a,a ║ ║ 4 ║ a,a,a ║ ║ 5 ║ a,b,a ║ ╚════╩══════════════╩ 下表是我想要实现的目标 ╔════╦

我想查找组_concat仅包含“a”的所有ID。 这是我的简化表

╔════╦══════════════╦
║ id ║ group_concat ║
╠════╬══════════════╬
║  1 ║ a,b,b        ║
║  2 ║ a            ║
║  3 ║ a,a          ║
║  4 ║ a,a,a        ║
║  5 ║ a,b,a        ║
╚════╩══════════════╩
下表是我想要实现的目标

╔════╦══════════════╦
║ id ║ group_concat ║
╠════╬══════════════╬
║  2 ║ a            ║
║  3 ║ a,a          ║
║  4 ║ a,a,a        ║
╚════╩══════════════╩
这就是我试图使用的查询语句

select id, group_concat(val)
from user
group by id
having group_concat(val) = 'a'
提前感谢

试试这个:

select id, group_concat(val)
from user
group by id
having count(distinct val) = 1 and max(val) = 'a'