mysql列字段别名,在MySQL中按别名选择列

I would like to understand certain specific behavior in MySQL. Running "select @@version", I see my version is 5.6.34-log.

Let me put the sample, using a generated table in order to make it easier to reproduce:

SELECT

CONCAT(a, b) AS 'c1', CONCAT((SELECT c1), 2)

FROM

(SELECT 'a', 'b', 'c' UNION ALL SELECT 1, 2, 3) t1;

What I was searching for initially was, as suggested by the title, how to select a column by its alias, in order to reuse calculated fields, avoiding long queries. Most of the answers either suggest using subqueries or variables - one has poor readability, and the other is not assured by the very own DB devs, as stated in documentation. Then, I learned this method from this answer, and can't quite understand it - indeed, I don't even know how to call this kind of operation/clause.

It seems to work very well, at least in this MySQL version. The only exception is when it comes to columns which contains aggregate functions (shown below) - it throws a 1247 (misreference) error, and that feels quite reasonable.

-- THIS DOESN'T WORK!

SELECT

CONCAT(a, b) AS c1, CONCAT((SELECT c1), 2) as c2

FROM

(SELECT 'a' as a, 'b' as b, 'c' as c UNION ALL SELECT '1', 2, 3) t1;

I've read many answers around this theme, but that is the only reference to this kind of operation, and, since I don't know its name, I can't look deeper into it. Does anyone know how is this structure called, how can I understand it better?

EDIT: I'm not trying to perform that operation shown in the not working query. Indeed, I'm trying to understand MySQL behavior. The already existing questions are enoug to understand how to do that using a subquery, and so on - that is not the point. My main issue is to understand what kind of operation is MySQL performing there and how is it called, since I've never read anything about something like that (would it be a query with an auto select?)

EDIT 2: This post has inspired a more specific and better written question about this MySQL behavior, which can be found here.

解决方案

Short answer:

references to aliases in SELECT list or

Aliased expressions

The only documentation I've found on this so far has been:

https://bugs.mysql.com/bug.php?id=79549

In that link there is the following:

[9 Dec 2015 15:35] Roy Lyseng

...

Here is a longer background for the original decision:

Contrary to references to aliases in subqueries in the WHERE clause (and in GROUP BY, for that matter), there is no reason (except standard compliance) that we should not allow references to aliases in the SELECT list, since they should be available in the same phase of query execution. But the support in 5.6 was quite arbitrary:

Given this: create table t1(a int, b int),

Alias in SELECT list is not valid:

select a+b as c,c+1 from t1;

ERROR 1054 (42S22): Unknown column 'c' in 'field list'

But within a subquery, reference to c is valid:

select a+b as c,(select c+1) from t1;

And subquery must be after definition of alias:

select (select c+1),a+b as c from t1;

ERROR 1247 (42S22): Reference 'c' not supported (forward reference in item list)

So, it is easy to say that support for references to aliases in SELECT list was rather ad-hoc. Nevertheless, we will try to reimplement the old solution, but with no attempt at cleaning up the obvious holes in the support for this feature. But referencing aliases in subqueries in the WHERE clause will not be reimplemented.

I'm yet looking for documentation beyond the bug report describing this functionality in the standard documents; but thus far no luck.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值