Tuesday, October 29, 2013

mysql group_concat,concat with join

select 
  a.id, 
  a.name,  
  group_concat(b.id) ids, 
  group_concat(b.title) titles,
  group_concat(concat("<a href='index.php?id=", b.id, "'>", b.title, "</a>")) titlelist
from participants a
inner join
  posts b on b.parentid = a.id
group by a.id,a.name
 
 
 
sample
 
id       Name       Value
1          A          4
1          A          5
1          B          8
2          C          9
 
 
result
 
id          Column
1          A:4,5,B:8
2          C:9  
 
select id, group_concat(`Name` separator ',') as `Column`
from
(
  select id, concat(`Name`, ':',
  group_concat(`Value` separator ',')) as `Name`
  from mytbl
  group by id, `Name`
) tbl
group by id; 

No comments: