mysql - 如何为 MySQL 中的字段或列设置别名?

标签 mysql subquery alias

我正在尝试做这样的事情。但我得到一个未知的列错误:

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/

相关文章:

PHP Mysql - 图像未在浏览器上显示(图像损坏)

Mysql UNION 作为带有别名字段的子查询

php - MySQL:规范化数据库的子查询

android - 无法获取 facebook sdk 哈希 key 。

bash - 您最重要的控制台别名是什么?

mysql - SQLSTATE[23000] : Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails laravel 5. 2

php - 从 MySQL 解析 JSON 字符串并将名称/值对插入 MySQL

MYSQL - SELECT AS 别名在子查询中使用

mysql - 错误代码: 1248. Every derived table must have its own alias 找不到查询的解决方案

mysql - 什么ip AWS lambda函数使用?