Skip to content
Advertisement

using condition for Group_Concat

I want to find all ids of which group_concat only contains ‘a’. Here is simplified table from mine.

╔════╦══════════════╦
║ id ║ group_concat ║
╠════╬══════════════╬
║  1 ║ a,b,b        ║
║  2 ║ a            ║
║  3 ║ a,a          ║
║  4 ║ a,a,a        ║
║  5 ║ a,b,a        ║
╚════╩══════════════╩

And the table below is what I want to achieve.

╔════╦══════════════╦
║ id ║ group_concat ║
╠════╬══════════════╬
║  2 ║ a            ║
║  3 ║ a,a          ║
║  4 ║ a,a,a        ║
╚════╩══════════════╩

And this is the query statement I am trying to use.

select id, group_concat(val)
from user
group by id
having group_concat(val) = 'a'

Thanks in advance

Advertisement

Answer

Try this:

select id, group_concat(val)
from user
group by id
having count(distinct val) = 1 and max(val) = 'a'
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement