2017-03-16 84 views
0

我真的運行下面這個查詢:如何連接MySQL查詢中的行?

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 

的問題是,我需要在它們各自的「service_id」內的列「tooth」中對這些行進行連接,嘗試了一切,但沒有成功,總共也是

回答

0

c.dente替換爲GROUP_CONCAT(c.dente SEPARATOR ' - '),並在下面添加GROUP BY service_id

+0

這與concat部分一起工作,但它仍然應該被拆分時,將服務名稱1和服務名稱2在同一行中混合使用。 – ayah

+0

我做了一個子查詢,在你的推薦,它的工作: (SELECT GROUP_CONCAT(c.dente SEPARATOR' - ')從labpedidoservicodente c其中b.id = c.idLabPedidoServico)作爲牙齒...它不是concat和它應該是分裂的。謝謝! – ayah

+0

@ayah,我編輯了我的答案並添加了「GROUP BY」 – tatskie