select * from self_join;

Untitled

EX 1) 대분류와 중분류를 함께 출력한 쿼리

SELECT A.category_type,
		   A.category_name,
       B.category_type,
       B.category_name
FROM self_join A,
		 self_join B
WHERE A.category_name = B.parent_category
  AND A.category_type = '대';

Untitled

EX 2) 대분류,중분류, 소분류를 함께 출력한 쿼리

SELECT A.category_type,
		   A.category_name,
       B.category_type,
       B.category_name,
       C.category_type,
       C.category_name
FROM self_join A,
		 self_join B,
     self_join C
WHERE A.category_name = B.parent_category
  AND B.category_name = C.parent_category;

Untitled

카테고리의 Depth가 깊어질수록 셀프 조인이 반복되는데, 계층 쿼리를 이용하면 좀 더 간단하게 쿼리를 작성할수 있다.

EX 3)