E
E
Eugene2018-08-08 22:09:36
SQL
Eugene, 2018-08-08 22:09:36

How to calculate sum in SQL query?

It is necessary to insert into table 1 the cost of the goods, which is equal to the sum of the cost of its spare parts.
product id 7 price=300+400+500
product id 6 price=200+300
For each id from table 2, calculate its cost and update it in table 1.
Is it possible to do this with an SQL query without data processing via php, and if so, how ?
Table 1 (data for both goods and spare parts)
id price
1 100
2 200
3 300
4 400
5 500
6 0 - after processing 500
7 0 - after processing 1200
Table 2 (Id of the product and Id of spare parts for it)
id id_zapchast
6 2
6 3
7 3
7 4
7 5

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan., 2018-08-08
@eugeneledenev

Something like this:

Select t1.id, sum(t2.price)
  From таблица2 as t1
  Inner join таблица1 as t2
              on t2.id = t1.id_zapchast
  group by t1.id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question