mysql - 如何在MySql中多次使用别名?

标签 mysql alias

我想在 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 in GROUP BY, ORDER BY, or HAVING clauses.

我认为不能在 SELECT 表达式中使用别名,以避免循环依赖和无效引用(当别名表达式包含 aggregate GROUP BY function 时)。

关于mysql - 如何在MySql中多次使用别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32107077/

相关文章:

mysql - 考虑到 NodeJS,MySQL 和 MySQL2 有什么区别

mysql - VB.net与MYSQL5连接

mysql - 在 HASH 索引上插入带有 PRIMARY KEY 的表的时间复杂度

php - 获取分组 Mysql 查询中的最后一个 id

MySQL JSON_OBJECT + CONCAT & GROUP_CONCAT 作为别名 & 截断/最大长度

ios - 在应用程序包中添加符号链接(symbolic link)

php - 如何在我的 php 页面中应用反向 ajax 来重新加载我列出的项目?

java - 如何通过xstream为对象别名?

subquery - HQL 子查询别名

r - 在 R 中构建包