简体   繁体   English

在一个mysql查询中使用concat和group_concat函数

[英]using concat and group_concat function in one mysql query

hi guys is it posible to use GROUP_CONCAT and CONCAT function in same query i am trying to use 嗨,大家好,我要使用同一查询中使用GROUP_CONCAT和CONCAT函数吗

SELECT GROUP_CONCAT(CONCAT(idmaterial,percent)) as 'material' FROM a_m where idarticle=1

to get result like this in one row one column 在一行一列中获得这样的结果

material
----------
1 5%10 6%80 1%10

please help me with your ideas thanks a lot here is my table thanks a lot for your helps 请帮我提出您的想法,非常感谢这里是我的餐桌,非常感谢您的帮助

    idarticle |idmaterial| percent
    ---------- ---------- ----------
    1              5        10
    ---------- ---------- ----------
    1              6        80    
    ---------- ---------- ----------
    1              1        10
    ---------- ---------- ----------
    2              1        90
    ---------- ---------- ----------
    2              2        10
    ---------- ---------- ----------

GROUP_CONCAT goes with GROUP BY : GROUP_CONCATGROUP BY

SELECT idarticle, 
  GROUP_CONCAT( CONCAT( idmaterial, '%', percent ) SEPARATOR ' ' ) as materials
FROM a_m
GROUP BY idarticle

And to put in one column you can use: 要放在一列中,您可以使用:

SELECT CAST(CONCAT( idarticle, ' ', 
  GROUP_CONCAT( CONCAT( idmaterial, '%', percent ) SEPARATOR ' ' ) ) AS CHAR) as material
FROM a_m
GROUP BY idarticle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM