2016-12-15 62 views
3

我要鏈接的表(總和(PRECO)和名稱從客戶端)SQL組2個不同的表

SELECT SUM(ARTIGO.PRECO) AS SUM_PRECO , CLIENTE.NOME_P 
FROM CLIENTE 
INNER JOIN ARTIGO 
ON ARTIGO.ID IN 
    (SELECT ARTIGO_ID FROM PERTENCERV2 WHERE COMPRACLIENTE_ID IN 
     (SELECT ID FROM COMPRACLIENTE WHERE CLIENTE_ID IN 
      (SELECT ID FROM CLIENTE))) 
WHERE CLIENTE.ID IN 
    (SELECT CLIENTE_ID FROM COMPRACLIENTE WHERE ID IN 
     (SELECT COMPRACLIENTE_ID FROM PERTENCERV2 WHERE ARTIGO_ID IN 
      (SELECT ID FROM ARTIGO))) 
GROUP BY CLIENTE.NOME_P; 

但是代碼的結果是上述

SUM_PRECO NOME_P    
    39 Joaquim    
    39 José     

和'39 「是的總和關閉所有價格 我如何可以隔離賬戶2個客戶端的

i added this image hope it can help understand

+0

請理想地與樣本數據一起張貼表結構。必須有一個更簡單的方法來做到這一點。 –

+0

artigo和客人餐桌之間有一條捷徑? –

+0

@JoeTaras i.stack.imgur.com/BPTFL.png,這是artigo和客戶之間的方式 – props

回答

2

您必須鏈接ARTIGOCLIENTE表與連接,從圖像中(請於把這裏的結構表,而像下一次)查詢是以下幾點:

試試這個:

SELECT SUM(a.preco) AS sum_preco , c.nome_p 
FROM cliente c 
JOIN compracliente cc 
    ON c.id = cc.cliente_id 
JOIN pertencerv2 p 
    ON p.compracliente_id = cc.id 
JOIN artigo a 
    ON a.id = p.artigo_id 
GROUP BY c.nome_p 

告訴我如果可以的話

+0

這就是它,謝謝 – props