mysql - 一列的多个别名

标签 mysql

我知道我可以为列指定一个临时别名。例如:

select foo, length(foo) as bar from my_table

而且我知道我可以使用这些别名来获取它们的值:

select foo, length(foo) as bar from my_table order by bar asc

但有时我想让我的查询结果更具可读性,当别名中有空格时,我不能在查询的其余部分使用它的值:

select foo, length(foo) as "Foo's length" from my_table order by ? asc

有没有一种方法可以为查询定义一个别名,并在结果中看到一个别名?

最佳答案

为什么这行不通:

select foo, length(foo) as `Foo's length`
from my_table
order by `Foo's length` asc;

或者,使用带有列引用的旧式 order by:

select foo, length(foo) as `Foo's length`
from my_table
order by 2 asc;

或者,重复表达式:

select foo, length(foo) as `Foo's length`
from my_table
order by length(foo) asc;

关于mysql - 一列的多个别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23583388/

相关文章:

java - 找不到适合 jdbc :mysql://localhost/的驱动程序

mysql - 连接被拒绝 - Laravel,远程数据库

mysql - 如何查询具有最低值的行,并知道最高值的值?

php - 将方法与 True 进行比较

mysql - 尝试在 MySQL 中选择 2 个联合字段作为一个字段

mysql - 使用group_concat后如何计算以 ','分隔的列的长度

php - 创建动态表单的解决方案

c++ - 异步 Mysql 连接器

mysql - 连接并设置两个表

MySQL - 如何加密密码