选择最短和最长字符串

ipakzgxi  于 2021-06-21  发布在  Mysql
关注(0)|答案(11)|浏览(646)

是否可以按表中的字符选择最短和最长的字符串?
我有一个 CITY 类型的列 VARCHAR(20) 我想选择最短和最长的城市名称,按长度按字母顺序排列。
我是这样做的 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; 当按字母顺序排列时,让城市名称列为、def、pqrs和wxy,长度分别为3、3、4和3。最长命名城市显然是pqrs,但最短命名城市有选择;我得选,因为它是按字母顺序排在第一位的。
我的查询结果是三个城市的长度都是3。 ABC 3 DEF 3 PQRS 4 WXY 3 select的结果必须是 ABC 3 PQRS 4

rnmwe5a2

rnmwe5a21#

我发现这个很简单:

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;
u4vypkhs

u4vypkhs2#

不管怎样,我得到了答案

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;
0sgqnhkj

0sgqnhkj3#

尝试这些查询。
最长城市名称:

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)
h79rfbju

h79rfbju4#

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;

上面的代码应该很容易理解。第一个查询显示最短的城市名称及其长度。第二个输出最长。

093gszye

093gszye5#

我读了上面midhun manohar的评论:https://stackoverflow.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://stackoverflow.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;
2nbm6dog

2nbm6dog6#

请查看以下查询:

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 ;
um6iljoc

um6iljoc7#

可以使用并集运算符

(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

goqiplq2

goqiplq28#

更干净的方式:

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;
wa7juj8i

wa7juj8i9#

我知道你已经选择了你的答案,但这里有一个简短的答案,可能会有所帮助。这是使用microsoftmysql服务器,但也可以使用calllimit而不是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;
yvfmudvl

yvfmudvl10#

我试过了,找到了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)

我认为用限制比用行号好。

jtjikinw

jtjikinw11#

我想你需要 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);

相关问题