大家好,是否可以在我尝试使用的同一查询中使用 GROUP_CONCAT 和 CONCAT 函数
SELECT GROUP_CONCAT(CONCAT(idmaterial,percent)) as 'material' FROM a_m where idarticle=1
在一行一列中获得这样的结果
material
----------
1 5%10 6%80 1%10
请帮助我提出您的想法,非常感谢,这是我的 table ,非常感谢您的帮助
idarticle |idmaterial| percent
---------- ---------- ----------
1 5 10
---------- ---------- ----------
1 6 80
---------- ---------- ----------
1 1 10
---------- ---------- ----------
2 1 90
---------- ---------- ----------
2 2 10
---------- ---------- ----------
最佳答案
GROUP_CONCAT
与 GROUP BY
搭配使用:
SELECT idarticle,
GROUP_CONCAT( CONCAT( idmaterial, '%', percent ) SEPARATOR ' ' ) as materials
FROM a_m
GROUP BY idarticle
要放入一列,您可以使用:
SELECT CAST(CONCAT( idarticle, ' ',
GROUP_CONCAT( CONCAT( idmaterial, '%', percent ) SEPARATOR ' ' ) ) AS CHAR) as material
FROM a_m
GROUP BY idarticle
关于mysql - 在一个mysql查询中使用concat和group_concat函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9332803/