gpt4 book ai didi

mysql - 合并为单个查询

转载 作者:行者123 更新时间:2023-11-29 10:56:07 26 4
gpt4 key购买 nike

查询 STATION 中 CITY 名称最短和最长的两个城市,以及它们各自的长度(即:名称中的字符数)。如果有多个最小或最大的城市,请选择按字母顺序排列的第一个城市。

SELECT CITY,CHAR_LENGTH(CITY) AS CHARLENGTH 
FROM STATION
ORDER BY CHARLENGTH ASC
LIMIT 1;

SELECT CITY,CHAR_LENGTH(CITY) AS CHARLENGTH
FROM STATION
ORDER BY CHARLENGTH DESC
LIMIT 1;

最佳答案

类似这样的吗?

##DROP TABLE STATION;
CREATE TABLE STATION (CITY VARCHAR(100));
INSERT INTO STATION VALUES ('aaaaaaa');
INSERT INTO STATION VALUES ('bbbbbbb');
INSERT INTO STATION VALUES ('cccc');
INSERT INTO STATION VALUES ('dd');
SELECT *
FROM
(SELECT CITY,CHAR_LENGTH(CITY) AS CHARLENGTH FROM STATION ORDER BY CHARLENGTH ASC, CITY LIMIT 1) A
UNION ALL
SELECT * FROM
(SELECT CITY,CHAR_LENGTH(CITY) AS CHARLENGTH FROM STATION ORDER BY CHARLENGTH DESC, CITY LIMIT 1) B

输出:

    CITY    CHARLENGTH
1 dd 2
2 aaaaaaa 7

关于mysql - 合并为单个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42996428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com