查询:用最短和最长的城市名称以及它们各自的长度(即名称中的字符数)查询STATION中的两个城市。如果有多个最小或最大的城市,请选择按字母顺序排列的第一个城市。在STATION中以最短和最长的城市名称查询两个城市,
样品输入:
比方说那个城市只有四个条目:DEF, ABC, PQRS and WXY
样本输出:
ABC 3
PQRS 4
查询:用最短和最长的城市名称以及它们各自的长度(即名称中的字符数)查询STATION中的两个城市。如果有多个最小或最大的城市,请选择按字母顺序排列的第一个城市。在STATION中以最短和最长的城市名称查询两个城市,
样品输入:
比方说那个城市只有四个条目:DEF, ABC, PQRS and WXY
样本输出:
ABC 3
PQRS 4
使用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。可以看看下面的只按字母顺序选择第一个记录的相关帖子:
这里是另一种方式来做到这一点使用总是得心应手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
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
按字母顺序给出结果的名字。
对于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;
试试这个:)
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;
@Moudiz它实际上是一个答案,只是没有使用代码格式 - 我编辑来解决这个问题。 – EJoshuaS
(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);
请提供一些解释,为什么这会回答问题 – CallumDA
欢迎来到Stack Overflow!尽管这段代码可以解决这个问题,但[包括一个解释](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)确实有助于提高您的帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性注释来挤占代码,这会降低代码和解释的可读性! – Carpetsmoker
@Carpetsmoker无论何时发布答案,都会添加解释。 –
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)
欢迎来到Stack Overflow!花一分钟阅读[如何回答](http://stackoverflow.com/questions/how-to-answer) - 这看起来很有帮助,但它会受益于代码的一些解释,考虑[编辑]( http://stackoverflow.com/posts/41385887/edit) - 在那? –
这看起来像一个练习题。显示你试图解决这个问题。 –
是的,我同意用vkp你试过了什么?提示你不需要嵌套或聚合函数,所以我解雇了那些。长度()是会给你的城市 – Matt
的长度(选择城市,长度(市) 从站 顺序按长度(市)ASC 限制1 )UNION ALL (选择城市,长度(城市功能) 从站 按长度排序(市)desc 限制1 ); –