Search code examples
mysqlgroup-by

mysql group by issues


Struggling with group by.

I have this query so far

select people.name, people.id, 
group_concat('org.org_id') as org_ids
from people
inner join org on org.vendor= people.vendor and org.inst_id = people.inst_id
group by people.id, org.org_id

It's giving me Unknown column org.org_id on field list

People table

id name vendor inst_id
1 Jorge walmart 7
2 Pete walmart 7
3 bert amazon 12
4 Omar amazon 2

Org table

org_id vendor inst_id
10 walmart 1
10 amazon 2
22 walmart 7
22 amazon 12
11 walmart 7
36 amazon 2

I'd like the results to be

name id org_id(s)
Jorge 1 22,11
Pete 2 22,11
bert 3 22
Omar 4 36,10

Solution

  • select people.name, people.id, 
    group_concat('org.org_id') as org_ids
    from people
    inner join org on org.vendor= people.vendor and org.inst_id = people.inst_id
    group by people.id, people.name
    

    edit after comments: I think the single quotes making the column a string must be your issue.

    select people.name, people.id, 
    group_concat( org.org_id ) as org_ids
    from people
    inner join org on org.vendor= people.vendor and org.inst_id = people.inst_id
    group by people.id, people.name