E
E
Eugene2019-09-17 22:01:42
MySQL
Eugene, 2019-09-17 22:01:42

How to write a complex SQL query correctly?

There are 3 tables
TABLE1 id, date, opt
TABLE1 id, date, opt
TABLE1 id, date, opt
TABLE2 id, num, qty, f1(->TABLE1.id), f2(->TABLE3.id) many entries for TABLE1
TABLE2 id, num, qty, f1(->TABLE1.id), f2(->TABLE3.id)
TABLE2 id, num, qty, f1(->TABLE1.id), f2(->TABLE3.id)
TABLE3 id, name
TABLE3 id, name
TABLE3 id, name
What you need: get the sum of the fields TABLE2.num*TABLE2.qty
Where TABLE1.date == current month AND TABLE.opt != NULL
Where TABLE2.f2 == TABLE3.id
group by TABLE3 .id

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman MySQL, 2019-09-17
@kevin

SELECT
 t3.`id`, SUM(t2.`num` * t2.`qty`)
FROM `table1` t1
JOIN `table2` t2 ON t2.`f1` = t1.`id`
JOIN `table3` t3 ON t3.`id` = t2.`f2`
WHERE t1.`date` >= '2019-09-01' AND NOT t1.`opt` IS NULL
GROUP BY t3.`id`

In fact, you made everything yourself. it was only necessary to translate from Russian to sql)) it is such a declarative language)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question