-- 카테고리 테이블을 작성한다.
DROP TABLE IF EXISTS category ;
CREATE TABLE category(
category_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
parent INT DEFAULT NULL);
CREATE TABLE category(
category_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
parent INT DEFAULT NULL);
INSERT INTO category
VALUES(1,'ELECTRONICS',NULL),(2,'TELEVISIONS',1),(3,'TUBE',2),
(4,'LCD',2),(5,'PLASMA',2),(6,'PORTABLE ELECTRONICS',1),
(7,'MP3 PLAYERS',6),(8,'FLASH',7),
(9,'CD PLAYERS',6),(10,'2 WAY RADIOS',6);
SELECT * FROM category ORDER BY category_id;
-- self 조인을 이용한 DEPTH ( LEVEL ) 구현 쿼리
-- 일반적인 방법이다.
SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4
FROM category AS t1
LEFT JOIN category AS t2 ON t2.parent = t1.category_id
LEFT JOIN category AS t3 ON t3.parent = t2.category_id
LEFT JOIN category AS t4 ON t4.parent = t3.category_id
WHERE t1.name = 'ELECTRONICS';
-- 레벨의 마지막 leaf node 만을 조회
SELECT t1.name FROM
category AS t1 LEFT JOIN category as t2
ON t1.category_id = t2.parent
WHERE t2.category_id IS NULL;
SELECT t1.name FROM
category AS t1 LEFT JOIN category as t2
ON t1.category_id = t2.parent
WHERE t2.category_id IS NULL;
-- 한개의 카테고리에 대해 상위 카테고리들 조회
SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4
FROM category AS t1
LEFT JOIN category AS t2 ON t2.parent = t1.category_id
LEFT JOIN category AS t3 ON t3.parent = t2.category_id
LEFT JOIN category AS t4 ON t4.parent = t3.category_id
WHERE t1.name = 'ELECTRONICS' AND t4.name = 'FLASH';
-- SELF 조인이 아닌 방법을 이용하기 위한 테이블 생성
DROP TABLE IF EXISTS nested_category ;
CREATE TABLE nested_category (
category_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
lft INT NOT NULL,
rgt INT NOT NULL
);
INSERT INTO nested_category
VALUES(1,'ELECTRONICS',1,20),(2,'TELEVISIONS',2,9),(3,'TUBE',3,4),
(4,'LCD',5,6),(5,'PLASMA',7,8),(6,'PORTABLE ELECTRONICS',10,19),
(7,'MP3 PLAYERS',11,14),(8,'FLASH',12,13),
(9,'CD PLAYERS',15,16),(10,'2 WAY RADIOS',17,18);
VALUES(1,'ELECTRONICS',1,20),(2,'TELEVISIONS',2,9),(3,'TUBE',3,4),
(4,'LCD',5,6),(5,'PLASMA',7,8),(6,'PORTABLE ELECTRONICS',10,19),
(7,'MP3 PLAYERS',11,14),(8,'FLASH',12,13),
(9,'CD PLAYERS',15,16),(10,'2 WAY RADIOS',17,18);
-- 당 카테고리가 지니고 있는 하위 카테고리 출력
SELECT node.name
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND parent.name = 'PORTABLE ELECTRONICS'
ORDER BY node.lft;
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND parent.name = 'PORTABLE ELECTRONICS'
ORDER BY node.lft;
-- 모든 마지막 레벨 조회
SELECT name
FROM nested_category
WHERE rgt = lft + 1;
SELECT name
FROM nested_category
WHERE rgt = lft + 1;
-- 해당 카테고리가 소속되는 상위 카테고리 출력
SELECT parent.name
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'FLASH'
ORDER BY parent.lft;
SELECT parent.name
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.name = 'FLASH'
ORDER BY parent.lft;
-- 카테고리별 TREE 의 레벨 출력
SELECT node.name, (COUNT(parent.name) - 1) AS depth
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY node.lft;
SELECT node.name, (COUNT(parent.name) - 1) AS depth
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY node.lft;
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7830 | 10년 전 | 448 | ||
| 7829 |
|
10년 전 | 634 | |
| 7828 | 10년 전 | 558 | ||
| 7827 | 10년 전 | 457 | ||
| 7826 | 10년 전 | 468 | ||
| 7825 | 10년 전 | 509 | ||
| 7824 | 10년 전 | 469 | ||
| 7823 | 10년 전 | 412 | ||
| 7822 | 10년 전 | 389 | ||
| 7821 | 10년 전 | 331 | ||
| 7820 | 10년 전 | 361 | ||
| 7819 |
|
10년 전 | 761 | |
| 7818 | 10년 전 | 411 | ||
| 7817 | 10년 전 | 571 | ||
| 7816 | 10년 전 | 429 | ||
| 7815 | 10년 전 | 626 | ||
| 7814 | 10년 전 | 465 | ||
| 7813 | 10년 전 | 417 | ||
| 7812 | 10년 전 | 428 | ||
| 7811 | 10년 전 | 410 | ||
| 7810 | 10년 전 | 605 | ||
| 7809 | 10년 전 | 539 | ||
| 7808 | 10년 전 | 413 | ||
| 7807 | 10년 전 | 433 | ||
| 7806 |
프로그래머7
|
10년 전 | 1356 | |
| 7805 | 10년 전 | 1298 | ||
| 7804 |
zahir1312
|
10년 전 | 795 | |
| 7803 |
|
10년 전 | 1396 | |
| 7802 | 10년 전 | 490 | ||
| 7801 | 10년 전 | 880 | ||
| 7800 | 10년 전 | 1101 | ||
| 7799 | 10년 전 | 582 | ||
| 7798 | 10년 전 | 534 | ||
| 7797 | 10년 전 | 546 | ||
| 7796 | 10년 전 | 384 | ||
| 7795 | 10년 전 | 538 | ||
| 7794 | 10년 전 | 581 | ||
| 7793 | 10년 전 | 1079 | ||
| 7792 | 10년 전 | 502 | ||
| 7791 | 10년 전 | 584 | ||
| 7790 | 10년 전 | 523 | ||
| 7789 |
fbastore
|
10년 전 | 1472 | |
| 7788 | 10년 전 | 574 | ||
| 7787 | 10년 전 | 432 | ||
| 7786 | 10년 전 | 635 | ||
| 7785 | 10년 전 | 617 | ||
| 7784 | 10년 전 | 672 | ||
| 7783 | 10년 전 | 485 | ||
| 7782 | 10년 전 | 520 | ||
| 7781 | 10년 전 | 923 | ||
| 7780 | 10년 전 | 839 | ||
| 7779 | 10년 전 | 793 | ||
| 7778 | 10년 전 | 377 | ||
| 7777 | 10년 전 | 487 | ||
| 7776 | 10년 전 | 486 | ||
| 7775 | 10년 전 | 425 | ||
| 7774 | 10년 전 | 640 | ||
| 7773 | 10년 전 | 397 | ||
| 7772 | 10년 전 | 764 | ||
| 7771 | 10년 전 | 417 | ||
| 7770 | 10년 전 | 664 | ||
| 7769 | 10년 전 | 417 | ||
| 7768 | 10년 전 | 636 | ||
| 7767 | 10년 전 | 1199 | ||
| 7766 | 10년 전 | 520 | ||
| 7765 | 10년 전 | 572 | ||
| 7764 |
잘살아보자
|
10년 전 | 423 | |
| 7763 |
|
10년 전 | 1486 | |
| 7762 |
Tosea
|
10년 전 | 1081 | |
| 7761 | 10년 전 | 678 | ||
| 7760 |
잘살아보자
|
10년 전 | 723 | |
| 7759 |
잘살아보자
|
10년 전 | 564 | |
| 7758 |
잘살아보자
|
10년 전 | 624 | |
| 7757 | 10년 전 | 1265 | ||
| 7756 |
ITBANK
|
10년 전 | 1277 | |
| 7755 | 10년 전 | 1940 | ||
| 7754 | 10년 전 | 1087 | ||
| 7753 | 10년 전 | 907 | ||
| 7752 | 10년 전 | 1413 | ||
| 7751 |
잘살아보자
|
10년 전 | 562 | |
| 7750 |
잘살아보자
|
10년 전 | 495 | |
| 7749 |
잘살아보자
|
10년 전 | 515 | |
| 7748 |
잘살아보자
|
10년 전 | 538 | |
| 7747 |
잘살아보자
|
10년 전 | 615 | |
| 7746 |
잘살아보자
|
10년 전 | 694 | |
| 7745 |
잘살아보자
|
10년 전 | 936 | |
| 7744 |
잘살아보자
|
10년 전 | 433 | |
| 7743 | 10년 전 | 961 | ||
| 7742 |
starbros
|
10년 전 | 856 | |
| 7741 |
잘살아보자
|
10년 전 | 696 | |
| 7740 |
잘살아보자
|
10년 전 | 581 | |
| 7739 |
잘살아보자
|
10년 전 | 478 | |
| 7738 |
잘살아보자
|
10년 전 | 558 | |
| 7737 |
잘살아보자
|
10년 전 | 534 | |
| 7736 |
잘살아보자
|
10년 전 | 551 | |
| 7735 |
잘살아보자
|
10년 전 | 884 | |
| 7734 |
잘살아보자
|
10년 전 | 443 | |
| 7733 |
잘살아보자
|
10년 전 | 560 | |
| 7732 |
잘살아보자
|
10년 전 | 720 | |
| 7731 |
잘살아보자
|
10년 전 | 640 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기