我是SQL新手,因此問題。Len中的Column查詢
因此,我用下面的表,以Id,城市,州名爲站。
我需要在STATION中以最短和最長的CITY名稱以及它們各自的長度(即名稱中的字符數)來查詢兩個城市。如果有多個最小或最大的城市,請選擇按字母順序排列的第一個城市。
有人可以幫我開始做這件事。我試過函數city
,但它似乎不工作。
我是SQL新手,因此問題。Len中的Column查詢
因此,我用下面的表,以Id,城市,州名爲站。
我需要在STATION中以最短和最長的CITY名稱以及它們各自的長度(即名稱中的字符數)來查詢兩個城市。如果有多個最小或最大的城市,請選擇按字母順序排列的第一個城市。
有人可以幫我開始做這件事。我試過函數city
,但它似乎不工作。
市最短名稱:
SELECT City, CHAR_LENGTH(City) AS len
FROM STATION
ORDER BY CHAR_LENGTH(City) ASC, City
LIMIT 1
市最長的名字:
SELECT City, CHAR_LENGTH(City) AS len
FROM STATION
ORDER BY CHAR_LENGTH(City) DESC, City
LIMIT 1
你可以將它們組合成一個單一的語句UNION ALL:
SELECT *
FROM (
SELECT City, CHAR_LENGTH(City) AS len
FROM STATION
ORDER BY CHAR_LENGTH(City) ASC, City
LIMIT 1
) shortest
UNION ALL (
SELECT City, CHAR_LENGTH(City) AS len
FROM STATION
ORDER BY CHAR_LENGTH(City) DESC, City
LIMIT 1
) longest
準確地說,我即將發佈:)這是一個 –
這裏是一個單一的查詢,你可以嘗試:
SELECT City, CHAR_LENGTH(City) AS length, 'max char length' AS description
FROM yourTable
WHERE CHAR_LENGTH(City) = (SELECT MIN(CHAR_LENGTH(City)) FROM yourTable)
ORDER BY City
LIMIT 1
UNION
SELECT City, CHAR_LENGTH(City) AS length, 'min char length' AS description
FROM yourTable
WHERE CHAR_LENGTH(City) = (SELECT MAX(CHAR_LENGTH(City)) FROM yourTable)
ORDER BY City
LIMIT 1
如果有多個最小或最大的城市,請選擇按字母順序排列的第一個城市。' –
是的,我跑這個查詢,它需要報告按字典順序排序最小或最大。 – Zeus
這裏是我的裂紋它。這是未經測試的,但它會給你一些開始。 (編輯:現在它已經過測試,它的工作原理可能會更好,但這將是使用子查詢的替代方案)。
SELECT
(SELECT City
FROM table
ORDER BY CHAR_LENGTH(City) DESC, City
LIMIT 1) AS longest,
(SELECT CHAR_LENGTH(City)
FROM table
ORDER BY CHAR_LENGTH(City) DESC, City
LIMIT 1) AS longest_length,
(SELECT City
FROM table
ORDER BY CHAR_LENGTH(City) ASC, City
LIMIT 1) AS shortest,
(SELECT CHAR_LENGTH(City)
FROM table
ORDER BY CHAR_LENGTH(City) ASC, City
LIMIT 1) AS shortest_length
FROM table
LIMIT 1
我不認爲,這是行得通的。選擇部分中的子選擇必須返回一列(AFAIK)。 –
好的,謝謝。我對它進行了編輯,以便將子查詢分割成每個選擇一列。 – kloddant
嘿得到了黑客排名:-P
同樣的問題,這是解決方案:
select * from
(select city, length(city) from station where length(city) = (select min(length(city)) from station) order by 1 limit 1) as tmp1
union all
select * from
(select city, length(city) from station where length(city) = (select max(length(city)) from station) order by 1 limit 1) as tmp2
我們不能子句UNION子句中使用限制和秩序。因此我們需要從查詢和應用UNION子句中取得結果的別名。
希望這將有助於:-)
(select CITY,length(CITY)
from STATION
where length(CITY)=(select min(length(CITY)) from STATION)
order by city
LIMIT 1)
union
(select CITY,length(CITY)
from STATION
where length(CITY)=(select max(length(CITY)) from STATION)
order by city
LIMIT 1);
雖然此代碼可能有助於解決問題,但 提供了有關_why_和/或_how_的其他上下文,回答此問題將顯着提高其長期價值 。請[編輯]你的答案,添加一些 的解釋。 –
(select CITY,length(CITY)
from STATION
where length(CITY)=(select min(length(CITY)) from STATION)
order by city
LIMIT 1)
union
(select CITY,length(CITY)
from STATION
where length(CITY)=(select max(length(CITY)) from STATION)
order by city
LIMIT 1)
這也可行 -
select first(CITY) ,len(first(CITY)) from STATION where CITY in (select CITY from STATION where len(CITY) in (SELECT MAX(LEN(CITY)) FROM STATION) order by CITY asc)
UNION
select first(CITY) ,len(first(CITY)) from STATION where CITY in (select CITY from STATION where len(CITY) in (SELECT MIN(LEN(CITY)) FROM STATION) order by CITY asc);
SELECT City, CHAR_LENGTH(City)
FROM STATION
ORDER BY CHAR_LENGTH(City) ASC, City
LIMIT 1;
SELECT City, CHAR_LENGTH(City)
FROM STATION
ORDER BY CHAR_LENGTH(City) DESC, City
LIMIT 1;
試試上面的解決方案,我估計應該解決您的問題。
這爲我工作:
(選擇城市,CHAR_LENGTH(CITY)FROM STATION ORDER BY CHAR_LENGTH(CITY)ASC LIMIT 1)UNION(選擇城市,CHAR_LENGTH(CITY)FROM STATION ORDER BY CHAR_LENGTH(CITY) DESC LIMIT 1)
MySQL中沒有'len()'函數 - 嘗試'length()'。 –
我正在嘗試這個鏈接,http://www.w3schools.com/sql/sql_func_len.asp – Zeus
只需用'ORDER BY LENGTH(City)ASC,City LIMIT 1'進行兩個查詢 - 一次使用ASC,一次使用DESC。 –