如何在MySQL查询中连接行?

时间:2017-03-16 00:05:14

标签: mysql database

我在下面运行此查询:

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'

我的结果就是这样:

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

我想要的输出应该是这样的:

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

问题是我需要在列#34;牙齿"中连接这些行。在他们各自的" service_id"里面,已经尝试了所有但没有成功,也是总计

1 个答案:

答案 0 :(得分:0)

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