Please Enable JavaScript!
Gon[ Enable JavaScript ]

UNION ALL 과 ORDER BY 섞어서 쓰기

기타 언어/데이터베이스(SQL)
반응형
각 게시판에 최근글을 모아서 리스트를 만들려고 시도했던 적이있다.
UNION ALL 과 ORDER BY 구문을 쓰다가 에러났는데 해결되었다.
-- 에러구문
(SELECT TOP 1 No, Title, Idate FROM tbCellPhoto ORDER BY Idate DESC
UNION ALL
SELECT TOP 1 No, Title, Idate FROM tbExchange ORDER BY Idate DESC)
ORDER BY Idate DESC

-- 문법
SELECT ....
FROM (SELECT TOP 1 ...
FROM ...
ORDER BY ...) AS x
UNION ALL
SELECT ....
FROM (SELECT TOP 4 ...
FROM ...
ORDER BY ... DESC) AS y

-- 다음과 같이 적용시켯다
SELECT TOP * FROM (
select * from (SELECT TOP 5 No, Title, Idate, path = '/Border/CellPhoto' FROM tbCellPhoto ORDER BY No DESC) AS a1
UNION ALL
select * from (SELECT TOP 5 No, Title, Idate, path = '/Border/Confession' FROM tbConfession ORDER BY No DESC) AS a2
) AS a3
ORDER BY Idate DESC
반응형
Posted by 녹두장군

댓글을 달아 주세요