2016-08-24 79 views
6

查詢:用最短和最長的城市名稱以及它們各自的長度(即名稱中的字符數)查詢STATION中的兩個城市。如果有多個最小或最大的城市,請選擇按字母順序排列的第一個城市。在STATION中以最短和最長的城市名稱查詢兩個城市,

樣品輸入:

比方說那個城市只有四個條目:DEF, ABC, PQRS and WXY

樣本輸出:

ABC 3 
PQRS 4 
+4

這看起來像一個練習題。顯示你試圖解決這個問題。 –

+1

是的,我同意用vkp你試過了什麼?提示你不需要嵌套或聚合函數,所以我解僱了那些。長度()是會給你的城市 – Matt

+0

的長度(選擇城市,長度(市) 從站 順序按長度(市)ASC 限制1 )UNION ALL (選擇城市,長度(城市功能) 從站 按長度排序(市)desc 限制1 ); –

回答

0

使用UNION試試這個:

SELECT MIN(city), LENGTH(city) 
FROM Station 
WHERE LENGTH(city) = 
(SELECT MIN(LENGTH(city)) 
FROM Station) 
UNION 
SELECT MIN(city), LENGTH(city) 
FROM Station 
WHERE LENGTH(city) = 
(SELECT MAX(LENGTH(city)) 
FROM Station) 

當然,我的假設是你的表名是Statio n和列名是City。可以看看下面的只按字母順序選擇第一個記錄的相關帖子:

Return only the first alphabetical result of SELECT

1

這裏是另一種方式來做到這一點使用總是得心應手row_number解析函數:

with cte as (
    select city, 
     length(city) as len, 
     row_number() over (order by length(city), city) as smallest_rn, 
     row_number() over (order by length(city) desc, city) as largest_rn 
    from station 
) 
select city, len 
    from cte 
where smallest_rn = 1 
union all 
select city, len 
    from cte 
where largest_rn = 1 
1
select min(city), len 
    from (
     select city, length(city) len, 
       max(length(city)) over() maxlen, 
       min(length(city)) over() minlen 
      from station 
     ) 
where len in(minlen,maxlen) 
group by len 

子查詢得到城市名單和它的長度。同時"window functions"min/max over()對於set(table)中的所有行獲得最小和最大長度。主要查詢過濾器只有城市的長度是最小/最大。 min(city)與羣組len按字母順序給出結果的名字。

+0

請提供您的代碼的解釋,以便OP可以從中學習。 – EBH

+1

@EBH好的。更新。我不說英文。如果它包含語法錯誤,請糾正我的答案。 – Mike

2

對於MS SQL服務器:

Declare @Small int 
Declare @Large int 
select @Small = Min(Len(City)) from Station 
select @Large = Max(Len(City)) from Station 
select Top 1 City as SmallestCityName,Len(City) as Minimumlength from Station where Len(City) = @Small Order by City Asc 
select Top 1 City as LargestCityName,Len(City) as MaximumLength from Station where Len(City) = @Large Order by City Asc 

對於Oracle服務器:

select * from(select distinct city,length(city) from station order by length(city) asc,city asc) where rownum=1 union 
select * from(select distinct city,length(city) from station order by length(city) desc,city desc) where rownum=1; 
3

試試這個:)

MySQL代碼....簡單的

select CITY,LENGTH(CITY) from STATION order by Length(CITY) asc, CITY limit 1; 
select CITY,LENGTH(CITY) from STATION order by Length(CITY) desc, CITY limit 1; 
+0

@Moudiz它實際上是一個答案,只是沒有使用代碼格式 - 我編輯來解決這個問題。 – EJoshuaS

3
(select CITY, 
     char_length(CITY) as len_city 
    from STATION 
    where char_length(CITY)=(select char_length(CITY) 
          from STATION 
          order by char_length(CITY) LIMIT 1) 
    Order by CITY LIMIT 1) 
UNION ALL 
(select CITY, 
     char_length(CITY) as len_city 
    from STATION 
    where char_length(CITY)=(select char_length(CITY) 
          from STATION 
          order by char_length(CITY) DESC LIMIT 1) 
    Order by CITY DESC LIMIT 1) 
    ORDER BY char_length(CITY); 
+3

請提供一些解釋,爲什麼這會回答問題 – CallumDA

+2

歡迎來到Stack Overflow!儘管這段代碼可以解決這個問題,但[包括一個解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高您的帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。也請儘量不要用解釋性註釋來擠佔代碼,這會降低代碼和解釋的可讀性! – Carpetsmoker

+0

@Carpetsmoker無論何時發佈答案,都會添加解釋。 –

1
SELECT TOP 1 CITY,LEN(CITY) from STATION 
    Where LEN(CITY)=(Select min(len(CITY)) from STATION) order by CITY 
SELECT TOP 1 CITY,LEN(CITY) from STATION 
    Where LEN(CITY)=(Select MAX(len(CITY)) from STATION) 
+3

歡迎來到Stack Overflow!花一分鐘閱讀[如何回答](http://stackoverflow.com/questions/how-to-answer) - 這看起來很有幫助,但它會受益於代碼的一些解釋,考慮[編輯]( http://stackoverflow.com/posts/41385887/edit) - 在那? –

相關問題