我正在尝试做这样的事情。但我得到一个未知的列错误:
SELECT SUM(field1 + field2) AS col1, col1 + field3 AS col3 from core
基本上,我只想使用别名,这样我就不需要执行之前执行的操作。这在mysql中可以吗?
最佳答案
select @code:= SUM(field1 + field2), @code+1 from abc;
但是,请注意以下事项(来自 MySQL 5.6 docs):
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.
因此,使用风险自负。
关于mysql - 如何为 MySQL 中的字段或列设置别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6081436/