在MySQL中连接两个group_concat查询
问题描述:
我想要做的事很奇怪!我想group_concat由两个不同列分组的列值之一。这将给我两个结果每列两列。之后,我想加入&从这两个结果concat group_concat值,以便我可以找到一个结果。 我的表在MySQL中连接两个group_concat查询
x y1 y2
----------
a 1 2
b 2 3
c 4 3
我想是这样
x y
----------
a 1
a,b 2
b,c 3
c 4
我可以通过GROUP_CONCAT Y1和Y2两种不同的查询分组的x值。我无法同时连接和连接它们。帮我。
答
我认为你只是想用union all
为“逆透视”的数据,然后做汇总:
select y, group_concat(x) as xs
from ((select x, y1 as y from t
) union all
(select x, y2 as y from t
)
) t
group by y
答
您可以通过
select x, group_concat(y) from (
select y1 as y, x
from my_table
union all
select y2, x
from mu_table) t
group by x
使用UNION ALL塔的选择组非常感谢你非常。你刚刚救了我的一天。我只是认为它有点复杂。 –