选择最短和最长的字符串
Posted
技术标签:
【中文标题】选择最短和最长的字符串【英文标题】:Select shortest and longest string 【发布时间】:2018-06-11 09:03:20 【问题描述】:是否可以通过表格中的字符来?
我有一个VARCHAR(20)
类型的CITY
列,我想按长度的字母顺序选择最短和最长的城市名称。
我喜欢这个
SELECT CITY,LENGTH(CITY) FROM STATION WHERE LENGTH(CITY) IN ( SELECT MAX(LENGTH(CITY)) FROM STATION UNION SELECT MIN(LENGTH(CITY)) FROM STATION ) ORDER BY CITY ASC;
按字母顺序排列时,让 CITY 名称列为 ABC、DEF、PQRS 和 WXY,长度分别为 3、3、4 和 3。名称最长的城市显然是 PQRS,但有以下选项名字最短的城市;我必须选择 ABC,因为它按字母顺序排在第一位。
我的查询以所有三个长度为 3 的 CITY 结束。
ABC 3 DEF 3 PQRS 4 WXY 3
SELECT 的结果必须是
ABC 3 PQRS 4
【问题讨论】:
LIMIT 1,获得至少2个城市?领带呢? 重复问题***.com/questions/35397411/… @LAVKUMARVISHWAKARMA,请注意,链接的问题是针对 mysql 以外的其他一些 dbms。 @jarlh mysql也有很多答案。 SQL query for finding the longest name and shortest name in a table的可能重复 【参考方案1】:反正我得到了答案
SELECT CITY,LENGTH(CITY)
FROM STATION
WHERE LENGTH(CITY) IN (
SELECT MAX(LENGTH(CITY))
FROM STATION
UNION
SELECT MIN(LENGTH(CITY))
FROM STATION
)
ORDER BY LENGTH(CITY) DESC,CITY ASC LIMIT 2;
【讨论】:
如果您有一个包含 6 个城市的表:XXX
、YYY
、ZZZ
、OOOO
、AAAAA
、BBBBB
?您的查询实际上变成了SELECT CITY, LENGTH(CITY) FROM STATION WHERE LENGTH(CITY) IN (5, 3) ORDER BY LENGTH(CITY) DESC, CITY ASC LIMIT 2;
您只会得到AAAAA
和BBBBB
作为结果,它们是两个最长的城市名称,而不是最长的和最短的。【参考方案2】:
我知道您已经选择了答案,但这里有一个简短的答案可能会有所帮助。 这是使用 Microsoft MySQL 服务器,但也可以使用调用 LIMIT 而不是 TOP 轻松转换为任何其他类型。
最短长度
SELECT TOP 1 CITY, LEN(CITY)
FROM STATION
ORDER BY LEN(CITY) ASC, CITY ASC;
最长的长度
SELECT TOP 1 CITY, LEN(CITY)
FROM STATION
ORDER BY LEN(CITY) DESC, CITY ASC;
【讨论】:
MySQL 和 Microsoft SQL Server 是不同的产品。【参考方案3】:试试这些查询。
最长的城市名称::
select CITY from STATION where char_length(CITY) = (select max(char_length(CITY)) from STATION)
最短城市名::
select CITY from STATION where char_length(CITY) = (select min(char_length(CITY)) from STATION)
【讨论】:
【参考方案4】:我已经尝试过了,找到了 MySQL 数据库查询的解决方案,
(SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) ASC, CITY ASC LIMIT 1)
UNION
(SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) DESC, CITY ASC LIMIT 1)
我认为使用 LIMIT 比使用行号更好。
【讨论】:
【参考方案5】:我看了上面 Midhun Manohar 的评论:https://***.com/a/50813334/11129060
我是一个新手,所以这些对我来说更容易理解:
SELECT CITY, LENGTH(CITY)
FROM STATION
WHERE LENGTH(CITY) IN (
SELECT MAX(LENGTH(CITY))
FROM STATION
)
ORDER BY CITY ASC LIMIT 1;
SELECT CITY, LENGTH(CITY)
FROM STATION
WHERE LENGTH(CITY) IN (
SELECT MIN(LENGTH(CITY))
FROM STATION
)
ORDER BY CITY ASC LIMIT 1;
或者另一个帖子似乎更容易:https://***.com/a/41285068/11129060
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;
【讨论】:
【参考方案6】:如果我理解正确,我认为您需要带有子查询的union
:
select s.*
from station s
where length(city) in (select max(length(city))
from station
union
select min(length(city))
from station)
order by length(city);
【讨论】:
我在您的回答@yogesh-sharma 的帮助下进行了编辑【参考方案7】:你可以使用 UNION 运算符
(SELECT CITY, MAX(LENGTH(CITY)) FROM STATION)
UNION
(SELECT CITY, MIN(LENGTH(CITY)) FROM STATION)
ORDER BY CITY ASC LIMIT 1;
更多例子https://dev.mysql.com/doc/refman/8.0/en/union.html
【讨论】:
【参考方案8】:请在下面的查询中找到这个:
SELECT top 2 CITY,LEN(CITY)
FROM STATION
WHERE LEN(CITY) IN (
SELECT MAX(LEN(CITY))
FROM STATION
UNION
SELECT MIN(LEN(CITY))
FROM STATION
)
ORDER BY LEN(CITY) DESC,CITY ASC ;
【讨论】:
请在您的答案中添加一些解释,以便其他人可以从中学习【参考方案9】:我发现这个很简单:
SELECT CITY, LENGTH(CITY) FROM (SELECT CITY FROM STATION ORDER BY LENGTH(CITY) ASC, CITY ASC)
WHERE ROWNUM=1;
SELECT CITY, LENGTH(CITY) FROM (SELECT CITY FROM STATION ORDER BY LENGTH(CITY) DESC, CITY ASC)
WHERE ROWNUM=1;
【讨论】:
【参考方案10】:更清洁的方式:
SELECT CITY, LENGTH(CITY) FROM STATION ORDER BY LENGTH(CITY) ASC, CITY ASC LIMIT 1;
SELECT CITY, LENGTH(CITY) FROM STATION ORDER BY LENGTH(CITY) DESC, CITY ASC LIMIT 1;
【讨论】:
【参考方案11】:在mssql server中,应该是这样的。
SELECT TOP 2 CITY, LEN(CITY) FROM STATION
WHERE LEN(CITY) IN (
SELECT MAX(LEN(CITY)) FROM STATION
UNION
SELECT MIN(LEN(CITY)) FROM STATION
)
ORDER BY LEN(CITY) DESC, CITY ASC;
【讨论】:
【参考方案12】:SELECT CITY,LENGTH(CITY)
FROM STATION
WHERE LENGTH(CITY) IN (
SELECT MAX(LENGTH(CITY))
FROM STATION
UNION
SELECT MIN(LENGTH(CITY))
FROM STATION
)
ORDER BY LENGTH(CITY) DESC,CITY ASC LIMIT 2;
【讨论】:
请在您的答案中添加一些解释,以便其他人可以从中学习 欢迎来到 Stack Overflow!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。【参考方案13】:(SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) ASC, CITY ASC LIMIT 1)
UNION
(SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) DESC, CITY DESC LIMIT 1);
这会给你这样的结果:
ABC 3
PQRS 4
【讨论】:
您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。【参考方案14】:答案可以在下面这个 sql 查询中简化。
(select city, length(city) from station group by city order by length(city), city limit 1)
union
(select city, length(city) from station group by city order by length(city) desc, city desc limit 1)
【讨论】:
【参考方案15】:For SQL Server
SELECT CITY,TXTLEN
FROM
(
SELECT RANK() OVER(ORDER BY CITY ASC) IDX,CITY ,LEN(CITY) TXTLEN FROM STATION
WHERE LEN(CITY) IN ( SELECT MIN (LEN(CITY)) FROM STATION)
UNION
SELECT RANK() OVER(ORDER BY CITY DESC) IDX,CITY ,LEN(CITY) TXTLEN FROM STATION
WHERE LEN(CITY) IN ( SELECT MAX (LEN(CITY)) FROM STATION)
) AS FIN WHERE IDX=1 ORDER BY TXTLEN
【讨论】:
正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。【参考方案16】:SELECT CITY, LENGTH(CITY) FROM STATION ORDER BY LENGTH(CITY),CITY LIMIT 1;
SELECT CITY, LENGTH(CITY) FROM STATION ORDER BY LENGTH(CITY) DESC, CITY LIMIT 1;
上面的代码应该很容易运行并且易于理解。第一个查询显示最短的城市名称及其长度。第二个输出最长。
【讨论】:
【参考方案17】:以下代码适用于 Oracle SQL。以上所有解决方案都不适用于 Oracle SQL 顺便说一句!您可以在两者之间添加 Union。但这也运行良好!
SELECT * FROM
(SELECT city,LENGTH(city)
from station
order by Length(city), city)
WHERE rownum <= 1;
SELECT * FROM
(SELECT city,LENGTH(city)
from station
order by Length(city) desc)
WHERE rownum <= 1;
【讨论】:
以上是关于选择最短和最长的字符串的主要内容,如果未能解决你的问题,请参考以下文章