SQL query for finding the longest name and shortest name in a table

后端 未结 29 2049
春和景丽
春和景丽 2021-01-31 00:11

I have a table with one of the columns is of type varchar(city). and want to find the longest and shortest of values stored in that column.

select a.city, a.city         


        
29条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 00:31

    This is another way of doing it in MySQL. May not be the best, but still be an option that is logically correct.

    select
       city,
       length(city) 
    from
       station 
    where
       length(city) in 
       (
          select
             max(length(city)) 
          from
             station 
          union
          select
             min(length(city)) 
          from
             station
       )
    order by
       length(city) desc,
       city asc limit 2;
    

提交回复
热议问题