有没有办法在mysql语句中重用计算字段.我收到错误"unknown column total_sale":
SELECT
s.f1 + s.f2 as total_sale,
s.f1 / total_sale as f1_percent
FROM sales s
Run Code Online (Sandbox Code Playgroud)
或者我必须重复计算,如果我添加了我需要的所有计算,这将产生一个非常长的SQL语句.
SELECT
s.f1 + s.f2 as total_sale,
s.f1 / (s.f1 + s.f2) as f1_percent
FROM sales s
Run Code Online (Sandbox Code Playgroud)
当然我可以在我的php程序中完成所有计算.
无论如何在同一个SELECT子句中使用列别名?例如:
SELECT ord_id, candy_id, price, quantity,
price * quantity AS ext_cost, ext_cost * @tax_rate
Run Code Online (Sandbox Code Playgroud)
返回错误,因为MySQL ext_cost在ext_cost * @tax_rate查询中无法识别" " .如果不可能,可以返回一个包含第一个查询中列出的所有内容的表,而不必写这样的内容吗?
SELECT ord_id, candy_id, price, quantity,
price * quantity AS ext_cost, (price * quantity) * @tax_rate
Run Code Online (Sandbox Code Playgroud)
基本上,我只是想知道无论如何都要ext_cost在SELECT查询中重用.