簡體   English   中英

如果多個記錄的長度相同,則選擇單個記錄

[英]selecting single record if multiple records are having same length

我正在嘗試在HackerRank上解決此問題

我試過這個查詢:

select city, length(city) from station 
where length(city) = (select max(length(city)) from station)
      or length(city) = (select min(length(city)) from station)
order by
length(city) ASC,city ASC;

運行上面的查詢后,我得到以下結果:

Amo 3 
Lee 3 
Roy 3 
Marine On Saint Croix 21

我的問題是:我只想選擇AmoMarine On Saint Croix 沒有其他人。 如何實現呢?

提前致謝

您可以嘗試選擇限制為1的最小和最大長度的城市並合並嗎?

select city, length(city) from station 
where  length(city) = (select max(length(city)) from station)
Limit  1
UNION
select city, length(city) from station 
where  length(city) = (select min(length(city)) from station)
Limit  1

這是有效的正確查詢:

 (SELECT city, LENGTH(city) AS length FROM station
ORDER BY LENGTH(city), city
LIMIT 1) UNION 

(SELECT city, LENGTH(city) AS length FROM station
ORDER BY LENGTH(city) DESC, city
LIMIT 1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM