我正在尝试解决this problem on 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
我的问题是:我只想选择Amo
& Marine On Saint Croix
。不是其他人。
怎么做到这一点?
先谢谢
答案 0 :(得分:1)
您可以尝试选择限制为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
答案 1 :(得分:0)
以下是正确的查询:
(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)