Is there a way to reuse a calculated field within a mysql statement. I get the error "unknown column total_sale" for:
SELECT
s.f1 + s.f2 as total_sale,
s.f1 / total_sale as f1_percent
FROM sales s
or do I have to repeat the calculation, which would make for a very long SQL statement if I added all the calculations I need.
SELECT
s.f1 + s.f2 as total_sale,
s.f1 / (s.f1 + s.f2) as f1_percent
FROM sales s
of course I can do all the calculations in my php program.
I have been testing the following and it seems to work all the time, maybe there is a reason for this, is it because I have predefined the variable @total_sales as being a value not a string, and have not redefined its type during the select statement ??
If I do the following