我想在 mysql
中使用此代码,但不知道如何使其工作:
SELECT
no1 + no2 + no3 as 'total',
'total' - no4 as 'arr',
(('total')+('arr')) * 2 / 100) as 'net'
FROM table
它显示了正确的结果,但我想要使用可以多次访问的别名的压缩表单:
SELECT
no1+ no2 +no3 as 'total',
(no1+ no2 +no3) - no4 as 'arr',
((no1+ no2 +no3) - 'no4') * 2 / 100 as 'per',
((no1+ no2 +no3) - 'no4') + (((no1+ no2 +no3) - 'no4') * 2 / 100) as 'net'
FROM table
最佳答案
简短的回答:这是不可能的。
引自 documentation
:
A select_expr can be given an alias using
AS alias_name
. The alias is used as the expression's column name and can be used inGROUP BY
,ORDER BY
, orHAVING
clauses.
我认为不能在 SELECT
表达式中使用别名,以避免循环依赖和无效引用(当别名表达式包含 aggregate GROUP BY function 时)。
关于mysql - 如何在MySql中多次使用别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32107077/