如何在 MySQL 中为字段或列设置别名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6081436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to alias a field or column in MySQL?
提问by user225269
I'm trying to do something like this. But I get an unknown column error:
我正在尝试做这样的事情。但我收到一个未知的列错误:
SELECT SUM(field1 + field2) AS col1, col1 + field3 AS col3 from core
Basically, I want to just use the alias so that I won't need to perform the operations performed earlier. Is this possible in mysql?
基本上,我只想使用别名,这样我就不需要执行之前执行的操作。这在mysql中可能吗?
采纳答案by Andomar
Consider using a subquery, like:
考虑使用子查询,例如:
SELECT col1
, col1 + field3 AS col3
FROM (
SELECT field1 + field2 as col1
, field3
from core
) as SubQueryAlias
回答by Ravi Parekh
select @code:= SUM(field1 + field2), @code+1 from abc;
select @code:= SUM(field1 + field2), @code+1 from abc;
But, please be aware of the following (from the MySQL 5.6 docs):
但是,请注意以下内容(来自MySQL 5.6 文档):
As a general rule, other than in SET statements, you should never assign a value to a user variable and read the value within the same statement. For example, to increment a variable, this is okay:
SET @a = @a + 1;
For other statements, such as SELECT, you might get the results you expect, but this is not guaranteed. In the following statement, you might think that MySQL will evaluate @a first and then do an assignment second:
SELECT @a, @a:=@a+1, ...;
However, the order of evaluation for expressions involving user variables is undefined.
作为一般规则,除了在 SET 语句中,您不应该为用户变量赋值并在同一语句中读取该值。例如,要增加一个变量,这是可以的:
SET @a = @a + 1;
对于其他语句,例如 SELECT,您可能会得到预期的结果,但这并不能保证。在下面的语句中,您可能认为 MySQL 会先评估 @a,然后再进行赋值:
SELECT @a, @a:=@a+1, ...;
但是,涉及用户变量的表达式的求值顺序是未定义的。
So, use at your own risk.
因此,使用风险自负。
回答by Moises
You can select
the alias:
您可以select
使用别名:
SELECT SUM(field1 + field2) AS col1, (select col1) + field3 AS col3 from core
This works.
这有效。
回答by mmavcy
According to the specRavi Parekh's answer is not guaranteed to always work, as "order of evaluation for expressions involving user variables is undefined".
根据规范Ravi Parekh 的答案不能保证总是有效,因为“涉及用户变量的表达式的评估顺序是未定义的”。
I found this answerafter I tried to use a variable and got weird results.
在尝试使用变量并得到奇怪的结果后,我找到了这个答案。
回答by Kamran Aslam
select @code:= SUM(field1 + field2), (@code*1) from abc;
@code*1 covert into numeric expression and you can use anywhere like
@code*1 转换成数字表达式,你可以在任何地方使用
select @code:= SUM(field1 + field2), (@code*1)+field3 from abc;
回答by Denis de Bernardy
Short answer is no:
简短的回答是否定的:
mysql> select 1 as a, a + 1 as b;
ERROR 1054 (42S22): Unknown column 'a' in 'field list'
postgresql# select 1 as a, a + 1 as b;
ERROR: column "a" does not exist
That said, some SQL implementations allow to use the aliases in where/group by/having clauses, e.g.:
也就是说,一些 SQL 实现允许在 where/group by/have 子句中使用别名,例如:
postgresql# select 1 as a group by a; -- 1 row
回答by ship shuk - www.shipshuk.com
In case you are using it with aggregate function (group by) and if it doesn't work for you place the calculated column to the end with forward column referecing.
如果您将它与聚合函数(分组依据)一起使用,并且它对您不起作用,请将计算出的列放置在前向列引用的末尾。
SELECT FNC2(AF), FNC1(A) AS AF, B, C, FROM Table GROUP BY ...
1st one doesn't work due to forward column referencing. Do this instead
SELECT FNC1(A) AS AF, B, C, FNC2((SELECT AF)) FROM Table GROUP BY ...