mysql 分组 列转行

SELECT aa.type,CONCAT('(',GROUP_CONCAT('\'',aa.user_id separator '\'\,'),'\')') FROM (select  aa.type,user_id FROM t_user_cust_info limit 2000) aa GROUP BY aa.type ;

1,把表中某一列的内容合并为一行

 select province,CONCAT('[\"全部\",\"',GROUP_CONCAT(city ORDER BY cityID separator '\"\,\"'),'\"]') as group1
from hat_city a
LEFT JOIN hat_province b on a.father=b.provinceID
GROUP BY father
ORDER BY cityID:根据指定字段排序,再合并
separator:指定分隔符,本文分隔符为(",")

 

1,把表中某一列的内容合并为一行

 select province,CONCAT('[\"全部\",\"',GROUP_CONCAT(city ORDER BY cityID separator '\"\,\"'),'\"]') as group1
from hat_city a
LEFT JOIN hat_province b on a.father=b.provinceID
GROUP BY father
ORDER BY cityID:根据指定字段排序,再合并
separator:指定分隔符,本文分隔符为(",")

猜你喜欢

转载自www.cnblogs.com/Struts-pring/p/9145471.html