작성
·
181
0
안녕하세요 문제에서는 각각 다른 쿼리로 작성가능하다고 하는데 하나의 쿼리로 구할수없을지 궁금합니다.
UNION을 사용하여 두개의 쿼리를 합칠려하는데 이방법이 맞는지 궁금합니다.
작성한 쿼리는 아래와 같습니다.
SELECT CITY, LENGTH(CITY)
FROM ( SELECT CITY, MIN(LENGTH(CITY))
FROM STATION
ORDER BY LENGTH(CITY),CITY
)AS A
UNION
SELECT CITY, LENGTH(CITY)
FROM (
SELECT CITY, MAX(LENGTH(CITY))
FROM STATION
ORDER BY LENGTH(CITY) DESC,CITY
)AS b
답변 1
0
강의에서 소개한 쿼리에서 세미콜론 대신 UNION을 쓰고 앞 뒤 쿼리들을 괄호로 묶어줄 경우 동일한 결과가 추출됩니다.
(SELECT city, LENGTH(city) length_city
FROM STATION
ORDER BY length_city, city
LIMIT 1)
UNION
(SELECT city, LENGTH(city) length_city
FROM STATION
ORDER BY length_city DESC, city
LIMIT 1)
UNION과 MIN, MAX 까지 작성하는 쿼리로는 아래와 같은 방법이 있을 수 있겠네요!
(SELECT city, LENGTH(city)
FROM station
WHERE LENGTH(city) = (SELECT MIN(LENGTH(city))
FROM station)
ORDER BY city
LIMIT 1)
UNION
(SELECT city, LENGTH(city)
FROM station
WHERE LENGTH(city) = (SELECT MAX(LENGTH(city))
FROM station)
ORDER BY city
LIMIT 1)