小编Mic*_* Xu的帖子

用于查找表中最长名称和最短名称的SQL查询

我有一个表,其中一列是varchar(city)类型.并希望找到该列中存储的最长和最短值.

select a.city, a.city_length from (select city, char_length(city) city_length 
from station order by city, city_length) a
where a.city_length = (select min(a.city_length) from a) or
      a.city_length = (select max(a.city_length) from a)
group by a.city_length;
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?谢谢


一个解决方案

select * from (select city, char_length(city) city_length from station order by city, city_length) a group by a.city_length order by a.city_length limit 1;
select * from (select city, char_length(city) city_length from station order by city, city_length) a group by a.city_length order by a.city_length desc limit 1;
Run Code Online (Sandbox Code Playgroud)

sql

13
推荐指数
5
解决办法
6万
查看次数

标签 统计

sql ×1