我想链接到表(sum(preco)和来自客户端的名称)
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”是所有价格的总和
我如何隔离两个客户的帐户
最佳答案
您必须通过图像将JOIN链接到ARTIGO
和CLIENTE
表(请在下一次将结构表放置在此处而不显示图像),查询如下:
尝试这个:
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
告诉我是否可以