Mysql 一列的多个别名
我知道我可以为列指定临时别名。例如:Mysql 一列的多个别名,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
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;
为什么这不起作用:
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;
为什么这不起作用:
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;
为什么这不起作用:
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;
用这个
select foo, length(foo) as "Foo's length" from my_table order by 2 asc
在按2排序的中,2是列索引
select foo, length(foo) as "Foo's length" from my_table order by 2 asc
在按2排序的中,2是列索引
select foo, length(foo) as "Foo's length" from my_table order by 2 asc
在按2排序的中,2是列索引
select foo, length(foo) as "Foo's length" from my_table order by 2 asc
在orderby2中,
2是列索引它不起作用的原因是MySQL对于双引号的含义有点精神分裂。有时表示字符串文字,有时表示列别名
当您像这样编写查询时
select foo, length(foo) as "Foo's length"
from my_table order by "Foo's length" asc
…它实际上将创建一个名为Foo's length
的列别名,但按字符串literalFoo's length
排序。由于所有行的字符串文字都相同,因此排序将是伪随机的
使用MySQL需要做的是使用backtick一致地引用别名,这样事情就会很好地工作
select foo, length(foo) as `Foo's length`
from my_table order by `Foo's length` asc
.它不起作用的原因是MySQL对于双引号的含义有点精神分裂。有时表示字符串文字,有时表示列别名
当您像这样编写查询时
select foo, length(foo) as "Foo's length"
from my_table order by "Foo's length" asc
…它实际上将创建一个名为Foo's length
的列别名,但按字符串literalFoo's length
排序。由于所有行的字符串文字都相同,因此排序将是伪随机的
使用MySQL需要做的是使用backtick一致地引用别名,这样事情就会很好地工作
select foo, length(foo) as `Foo's length`
from my_table order by `Foo's length` asc
.它不起作用的原因是MySQL对于双引号的含义有点精神分裂。有时表示字符串文字,有时表示列别名
当您像这样编写查询时
select foo, length(foo) as "Foo's length"
from my_table order by "Foo's length" asc
…它实际上将创建一个名为Foo's length
的列别名,但按字符串literalFoo's length
排序。由于所有行的字符串文字都相同,因此排序将是伪随机的
使用MySQL需要做的是使用backtick一致地引用别名,这样事情就会很好地工作
select foo, length(foo) as `Foo's length`
from my_table order by `Foo's length` asc
.它不起作用的原因是MySQL对于双引号的含义有点精神分裂。有时表示字符串文字,有时表示列别名
当您像这样编写查询时
select foo, length(foo) as "Foo's length"
from my_table order by "Foo's length" asc
…它实际上将创建一个名为Foo's length
的列别名,但按字符串literalFoo's length
排序。由于所有行的字符串文字都相同,因此排序将是伪随机的
使用MySQL需要做的是使用backtick一致地引用别名,这样事情就会很好地工作
select foo, length(foo) as `Foo's length`
from my_table order by `Foo's length` asc
.空格、撇号等-只是一个糟糕的想法请注意标记不是关键字。在标签列表中填入与你的问题相同的单词无助于分类。务必阅读选择标签时出现的说明!空格、撇号等-这是个糟糕的想法请注意标记不是关键字。在标签列表中填入与你的问题相同的单词无助于分类。务必阅读选择标签时出现的说明!空格、撇号等-这是个糟糕的想法请注意标记不是关键字。在标签列表中填入与你的问题相同的单词无助于分类。务必阅读选择标签时出现的说明!空格、撇号等-这是个糟糕的想法请注意标记不是关键字。在标签列表中填入与你的问题相同的单词无助于分类。务必阅读选择标签时出现的说明!数字和重复表达式形式的列引用对我来说是新的,但backticks。。。(脸掌)。谢谢:)以数字和重复表达式形式的列引用对我来说是新的,但是backticks。。。(脸掌)。谢谢:)以数字和重复表达式形式的列引用对我来说是新的,但是backticks。。。(脸掌)。谢谢:)以数字和重复表达式形式的列引用对我来说是新的,但是backticks。。。(脸掌)。谢谢:)