使用order by和limit在sql中获取错误

9udxz4iz  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(213)

当我在mysql中运行下面的sql查询时

select distinct city, length(city) from station order by length(city), order by city asc limit 1;

我犯了个错误

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use 
near 'order by city asc limit 1' at line 1

我不明白我在哪里犯了这个错误

7lrncoxx

7lrncoxx1#

只允许一个order by,然后用逗号分隔列名,如

order by length(city), city asc
91zkwejq

91zkwejq2#

你有 order by 两次。大概你想要:

select distinct city, length(city)
from station
order by length(city), city asc
limit 1;

也就是说,一个 order by 可以有多个键。

相关问题