SELECT Y/X as Z
FROM (
SELECT
(count(county) FROM `cleanup_site_list_2011` WHERE county='kings') as X,
(count(county) FROM `cleanup_site_list_2011` WHERE county='kings' AND people != 0) as Y
FROM `cleanup_site_list_2011`
) as innertable
In the above example the two “SELECT …” represent nested queries of any description (X and Y will be integers). When I try to use this statement I get the error
#1054 - Unknown column 'Y' in 'field list'
How would I get MySQL to recognize the names of the columns I created? Also, once the math works out, how would I make the division result in a float? Or does it do this automatically?
One way to deal with this is to give the columns their ‘custom names’ in a subquery, then use those custom names in the surrounding query:
For the second question:
Does that answer it? (MySQL 5)
More information here.