Reputation: 2795
I created a test database so I can illustrate my problem:
create table A(
id int(11) primary key not null,
price decimal(10,2)
);
create table B(
id int(11) primary key not null,
id_a int(11) not null,
foreign key(id_a) references A(id)
on update cascade
on delete restrict
);
insert into A values
(1,25),
(2,30),
(3,35);
insert into B values
(1,1),
(2,1),
(3,2),
(4,2),
(5,3);
That is a simplified example of some articles(A) and their prices, and a bill(B) on which there is id of bill and foreign key that represent what article is bought.
I need query to find profit from all sold articles. So to go through table B and and find the sum of all prices of sold articles.
Upvotes: 1
Views: 38
Reputation: 311393
You could just join the two tables:
SELECT SUM(price)
FROM a
JOIN b ON b.id_a = a.id
Upvotes: 2