T
T
technomad2021-04-02 11:53:27
SQL
technomad, 2021-04-02 11:53:27

How to make SQL query on 2 tables using SUM()?

Hello, there are 2 tables, table1 and table2 In table1

there are columns model, quantity, manufacturer_id In table2 there are
columns order_id, model ,

sale_quantity quantity, manufacturer_id iphone, 10, apple table2 order_id , model, sale_quantity 1, iphone, 1 2, iphone, 5 3, iphone, 7 That is, I want to get this: iphone,10, 13 I write such a request and get nonsense, it displays 1 line:






SELECT `table1`.`model`, `table1`.`quantity`, SUM(table2.sale_quantity)
FROM `table1`, `table2`
WHERE `table1`.`manufacturer_id` LIKE 'apple'
AND `table1`.`model`=`table2`.`model`
AND `table1`.`quantity` <20

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VitalyChaikin, 2021-04-02
@technomad

SELECT <Список полей1>, SumFromT2
FROM table1
LEFT JOIN 
   ( SELECT model, SUM(sale_quantity) AS SumFromT2 FROM table2 GROUP BY model) AS T2 
   ON table1.model = T2.model
WHERE <Уловия ограничивающие финальную выборку>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question