提问者:小点点

在带有ORDER BY和LIMIT的SQL中获取错误


当我在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

我不明白我在哪里犯的错


共2个答案

匿名用户

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

order by length(city), city asc 

匿名用户

您有两次order by。 想必你想:

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

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