简体   繁体   中英

How to concat rows in a MySQL query?

I`m running this query below:

SELECT a.id as id_order, b.id as id_service,
   d.nome as service_name, 
   c.dente as tooth,
   (SELECT count(c.dente) 
    FROM labpedidoservicodente c 
    WHERE b.id = c.idLabPedidoServico) AS total,
    e.valorServico as cost
FROM   labpedido a 
   INNER JOIN labpedidoservico b 
           ON a.id = b.idLabPedido 
   INNER JOIN labpedidoservicodente c 
           ON a.id = c.idLabPedido 
   INNER JOIN labservico d 
           ON b.idLabServico = d.id 
   INNER JOIN labservicovalor e
           ON b.idLabServico = e.idLabServico
WHERE  a.id = '914'

My result comes this way:

order_id  service_id  service_name     tooth   total   cost
914          640      SERVICE NAME 1     11      3    80.00
914          640      SERVICE NAME 1     21      3    80.00
914          640      SERVICE NAME 1     38      3    80.00
914          641      SERVICE NAME 2     11      3    84.80
914          641      SERVICE NAME 2     21      3    84.80
914          641      SERVICE NAME 2     38      3    84.80

My desired output should be like this:

order_id  service_id  service_name     tooth   total   cost
914          640      SERVICE NAME 1    11-21    2    80.00
914          641      SERVICE NAME 2     38      1    84.60

The problem is that I need to concat these rows in the column "tooth" inside their respective "service_id", have tried everything but no sucess, also the total

c.dente替换为GROUP_CONCAT(c.dente SEPARATOR ' - ')并在下面添加GROUP BY service_id

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