Zia
Zia

Reputation: 2710

Arithmetic via subquery?

How do you merge these two rows?

"SELECT col1, col2, col3, col4 FROM table_a WHERE col4 = '$col4_val'"

and

"SELECT col1 FROM table_a WHERE col5 = '$col4_val'"

say the first query col1 = 20 and the second query col1 = 10

how do I create a query that returns the values of col1,col2,col3,col4 from the first query but col1 = 30 (first and second query added together)?

I can do the arithmetic via php but I would prefer to do it in the query if possible.

Upvotes: 2

Views: 461

Answers (2)

Zia
Zia

Reputation: 2710

ok nevermind I figured it out..

"SELECT sum(col1 + (SELECT col1 FROM table_b WHERE col5 = '$col4_val')) as col1, col2, col3, col4 FROM table_a WHERE col4 = '$col4_val'"

Upvotes: 0

Kyra
Kyra

Reputation: 5407

"SELECT (a.col1 + b.col1) as 'summed value', a.col2, a.col3, a.col4 
FROM table_a as a 
JOIN table_b as b 
ON a.col4 = b.col5 
WHERE a.col4 = '$col4_val'"

Upvotes: 6

Related Questions