内部联接4个表与组,order by,有条款

时间:2015-04-30 08:59:48

标签: mysql sql sql-server

我有4个表,我想提取:idnumelocalitatemasina_idnr_inmatricularean_fabricatie,{ {1}},rafinariemarca总和(quantity+deliver_quantity)分组total_quantity,按an_fabricatie排序,并添加一些marca

我不知道如何做到这一点。

我的查询如下,但我认为不正确。

having clouse

1 个答案:

答案 0 :(得分:1)

SELECT c.id, c.nume, c.localitate, l.masina_id, i.nr_inmatriculare, i.an_fabricatie, i.rafinarie, m.marca, (SUM(i.quantity) + SUM(l.deliver_quantity)) AS total_quantity
FROM clienti c 
INNER JOIN livrari l ON c.id = l.id 
INNER JOIN incarcari i ON l.incarcare_id = i.livrari_id 
INNER JOIN masini m ON i.id_marca = m.id
GROUP BY i.an_fabricatie, c.id, c.nume,c.localitate,l.masina_id, i.nr_inmatriculare, i.rafinarie, m.marca
HAVING i.quantity > 1000 
ORDER BY m.marca DESC;