我有4个表,我想提取:id
,nume
,localitate
,masina_id
,nr_inmatriculare
,an_fabricatie
,{ {1}},rafinarie
和marca
总和(quantity+deliver_quantity)
分组total_quantity
,按an_fabricatie
排序,并添加一些marca
。
我不知道如何做到这一点。
我的查询如下,但我认为不正确。
having clouse
答案 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;