I am trying to sum two separate columns from two separate columns in one query.
So I have
t1
id - value
1 -10
2 - 20
3 - 15
4 - 40
5 -100
t2
id - parent_id - value
1 - 1 - 15
2 - 2 - 5
3 - 3 - 0
4 - 4 - 30
5 - 5 - 5
6 - 5 - 5
I'd like to get the results sum(t1.value)=185, sum (t2.value)=60 but when I run this query:
SELECT sum(t1.value),sum (t2.value) FROM t1 LEFT JOIN t2 ON t1.id=t2.parent_id
I get sum(t1.value)=285, sum (t2.value)=60, i.e. it's counting t1.value twice for t1.id=5 as this appears twice in t2.parent_id.
Can anyone help, or must I use two queries?