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

后端 未结 29 2064
春和景丽
春和景丽 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:35

    Initially finding the shortest length of the city and taking an union with the longest length of the city. This minimizes the complexity of the query.

    (select city, char_length(city) as len_city
    from station
    order by len_city limit 1)
    union ( select city, char_length(city) as len_city
        from station
        order by len_city desc limit 1) 
    order by len_city
    

提交回复
热议问题