Sqlite> UNION에 RANDOM() 적용

개발 기록 남기기·2022년 7월 8일
0

SQL

목록 보기
1/1

문제상황

select a, b, c 
from table_A 
order by random()
union all
select a, b, c 
from table_B 
order by random()

UNION으로 합쳐진 정보를 랜덤으로 정렬하기 위해 ORDER BY RANDOM() 사용시 다음과 같은 에러 발생.
1st ORDER BY term does not match any column in the result set

해결 방법

데이터를 검색하는 쿼리를 from절의 서브쿼리로 만듦

select * from ( 
   select a, b, c
   from table_A
   order by random()
) 
union all 
select * from ( 
   select a, b, c
   from table_B 
   order by random() 
)

물론 두 합쳐지는 두 쿼리의 select 대상은 같아야함.

profile
유니티, C# 개발자

0개의 댓글