如何从相同的table查询城市名称以及最小城市名称的长度
How to query for city name as well as length of the smallest city name from the same table
我的代码是这样的,但是报错,不知道为什么。请帮忙!
select city,
min(length(city))
from station
group by length(city)=min(length(city))
order by city asc;
如果您只想要名称最短的城市,您可以简单地 order by
和 limit
:
select city, char_length(city) city_length
from station
order by city_length
limit 1
这returns只是一行。另一方面,如果你想允许底部关系,那么你可以使用子查询进行过滤,如下所示:
select city, char_length(city) city_length
from station
where char_length(city) = (select min(char_length(city)) from station)
我的代码是这样的,但是报错,不知道为什么。请帮忙!
select city,
min(length(city))
from station
group by length(city)=min(length(city))
order by city asc;
如果您只想要名称最短的城市,您可以简单地 order by
和 limit
:
select city, char_length(city) city_length
from station
order by city_length
limit 1
这returns只是一行。另一方面,如果你想允许底部关系,那么你可以使用子查询进行过滤,如下所示:
select city, char_length(city) city_length
from station
where char_length(city) = (select min(char_length(city)) from station)