开发者

Arithmetic via subquery?

开发者 https://www.devze.com 2023-01-04 09:48 出处:网络
How do you merge these two rows? \"SELECT col1, col2, col3, col4 FROM table_a WHERE col4 = \'$col4_val\'\"

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 re开发者_如何学编程turns 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.


"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'"


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'"
0

精彩评论

暂无评论...
验证码 换一张
取 消