先看下面的简单sql
SELECT 1 AS one, 2 AS two, null AS three UNION ALL SELECT 1 AS one, null AS two, 3 AS three
结果:
但是如果我想以one字段为合并条件,其余字段没有值的不填,有值的补上该怎么写,即我想要的结果是1,2,3。这边会用到group_concat()函数,完整sql:
SELECT
one,
GROUP_CONCAT(two) AS two,
GROUP_CONCAT(three) AS three
FROM
( SELECT 1 AS one, 2 AS two, null AS three UNION ALL SELECT 1 AS one, null AS two, 3 AS three ) AS temp
GROUP BY
one
结果: