Mysql
 sql >> Database >  >> RDS >> Mysql

Come ottenere un set di dati da una query SQL da più tabelle?

Proverò a rispondere usando solo SQL.

Usa group_concat con distinti,

select t.id,name,phone,
 group_concat(distinct a.addr separator ',') as address,
 group_concat(distinct c.cat separator ',') as category
  from table1 t
  left join address a 
    on t.id = a.refid
  left join category c
    on t.id = c.refid
  group by t.id,name, phone

O

Come colonna in linea,

select t.id,name,phone,
 (select group_concat(a.addr separator ',')
    from address a 
   where a.refid = t.id) as address,
 (select group_concat(c.cat separator ',') 
    from category c
   where c.refid = t.id) as category
  from table1 t

Riferimento Db<>violino