join - Mysql query to concat rows -


mysql tables structure is

b1 //table 1

aid         name      1            aaa 2            bbb 3            ccc 4            ddd 

b2 //table 2

bid         aid 111          1 222          2 333          3 444          3 555          4 

i want display data following

aid      bid             name 1        111              aaa 2        222              bbb 3        333,444          ccc 4        555              ddd 

i tried not getting result expected. how using subquery or joins?

thank you.

you can use group_concat

select     b1.aid,     group_concat(distinct b2.bid) bids,     b1.name b1 left join b2 on b1.aid = b2.aid group b1.aid 

fiddle demo

output

| aid |    bids | name | |-----|---------|------| |   1 |     111 |  aaa | |   2 |     222 |  bbb | |   3 | 333,444 |  ccc | |   4 |     555 |  ddd | 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -