简体   繁体   中英

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

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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