mysql - Can data from a subquery be used in the outer query? -


sorry poor question title, i'm not sure if there's name i'm trying do.

i have query such following:

select a, b, c, (d + e - f) computedvalue,  (select sum(column1) table2) d, (select sum(column2) table3) e, (select sum(column3) table4) f,  table1 = 1 

so, in other words, use integer values returned subqueries compute value. can in php this:

$computedvalue = $row['d'] + $row['e'] - $row['f']; 

but i'm wondering if it's possible in query itself?

when tried got following error:

error!: sqlstate[42s22]: column not found: 1054 unknown column 'd' in 'field list'

you can't use column aliases in clauses of same query, including in select-list.
seems puzzling many sql developers, that's standard sql.

but can use derived table subquery this:

select *, (d + e - f) computedvalue  (     select a, b, c,         (select sum(column1) table2) d,     (select sum(column2) table3) e,     (select sum(column3) table4) f         table1     = 1 ) x; 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -