views:

131

answers:

1

Is there an elegant way to do this in MySQL:

SELECT (subquery1) AS s1, (subquery2) AS s2, (s1+s2) AS s3

or must I resort to

SELECT (subquery1) AS s1, (subquery2) AS s2, ((subquery1)+(subquery2)) AS s3

?

Thanks

EDIT: both subqueries yield integer results

+1  A: 

You can use variables within MySQL


SELECT @query1:=(subquery) AS s1, @query2:=(subquery) AS s2, (@query1+@query2) AS s3

Still not that elegant. Perhaps you could elaborate on the subqueries so we could perhaps suggest better ways?

rezzif
this is great, actually, but does it evaluate the subquery each time, or only the first time when set to a variable?
Jason
by my understanding it should only evaluate it once when it is put into the variable. You can actually create the vars in a different query and they persist for the connection. http://dev.mysql.com/doc/refman/5.0/en/user-variables.html
rezzif
fantastic. amazing. thank you.
Jason