-- 카테고리 테이블을 작성한다.
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;
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7730 | 10년 전 | 1312 | ||
| 7729 | 10년 전 | 1156 | ||
| 7728 |
잘살아보자
|
10년 전 | 607 | |
| 7727 |
잘살아보자
|
10년 전 | 502 | |
| 7726 |
잘살아보자
|
10년 전 | 833 | |
| 7725 |
잘살아보자
|
10년 전 | 563 | |
| 7724 |
잘살아보자
|
10년 전 | 474 | |
| 7723 |
잘살아보자
|
10년 전 | 539 | |
| 7722 |
잘살아보자
|
10년 전 | 481 | |
| 7721 |
잘살아보자
|
10년 전 | 512 | |
| 7720 |
잘살아보자
|
10년 전 | 472 | |
| 7719 |
비긴어게인
|
10년 전 | 694 | |
| 7718 |
|
10년 전 | 2543 | |
| 7717 |
잘살아보자
|
10년 전 | 657 | |
| 7716 |
잘살아보자
|
10년 전 | 407 | |
| 7715 |
잘살아보자
|
10년 전 | 435 | |
| 7714 |
잘살아보자
|
10년 전 | 502 | |
| 7713 | 10년 전 | 1789 | ||
| 7712 | 10년 전 | 1719 | ||
| 7711 | 10년 전 | 1107 | ||
| 7710 | 10년 전 | 1406 | ||
| 7709 | 10년 전 | 1525 | ||
| 7708 | 10년 전 | 1463 | ||
| 7707 | 10년 전 | 860 | ||
| 7706 |
별지기천사
|
10년 전 | 573 | |
| 7705 | 10년 전 | 1083 | ||
| 7704 |
ICONdesignstudio
|
10년 전 | 627 | |
| 7703 | 10년 전 | 597 | ||
| 7702 |
|
10년 전 | 734 | |
| 7701 | 10년 전 | 1423 | ||
| 7700 | 10년 전 | 1113 | ||
| 7699 | 10년 전 | 584 | ||
| 7698 | 10년 전 | 1149 | ||
| 7697 | 10년 전 | 5171 | ||
| 7696 | 10년 전 | 663 | ||
| 7695 | 10년 전 | 1690 | ||
| 7694 | 10년 전 | 1066 | ||
| 7693 | 10년 전 | 1561 | ||
| 7692 | 10년 전 | 1303 | ||
| 7691 | 10년 전 | 829 | ||
| 7690 | 10년 전 | 1394 | ||
| 7689 | 10년 전 | 1022 | ||
| 7688 | 10년 전 | 623 | ||
| 7687 |
파랑새1597
|
10년 전 | 602 | |
| 7686 | 10년 전 | 861 | ||
| 7685 | 10년 전 | 1346 | ||
| 7684 | 10년 전 | 804 | ||
| 7683 | 10년 전 | 1106 | ||
| 7682 | 10년 전 | 1033 | ||
| 7681 | 10년 전 | 673 | ||
| 7680 | 10년 전 | 992 | ||
| 7679 | 10년 전 | 512 | ||
| 7678 | 10년 전 | 746 | ||
| 7677 | 10년 전 | 644 | ||
| 7676 |
|
10년 전 | 951 | |
| 7675 |
|
10년 전 | 1182 | |
| 7674 | 10년 전 | 1056 | ||
| 7673 | 10년 전 | 756 | ||
| 7672 | 10년 전 | 1095 | ||
| 7671 | 10년 전 | 895 | ||
| 7670 | 10년 전 | 666 | ||
| 7669 |
mashmellow
|
10년 전 | 1236 | |
| 7668 | 10년 전 | 724 | ||
| 7667 | 10년 전 | 1019 | ||
| 7666 |
senseme
|
10년 전 | 661 | |
| 7665 | 10년 전 | 517 | ||
| 7664 | 10년 전 | 1900 | ||
| 7663 |
mixx애교
|
10년 전 | 985 | |
| 7662 | 10년 전 | 1047 | ||
| 7661 |
hkhkah
|
10년 전 | 798 | |
| 7660 | 10년 전 | 1075 | ||
| 7659 |
커네드커네드
|
10년 전 | 939 | |
| 7658 |
바람돌이팡
|
10년 전 | 680 | |
| 7657 | 10년 전 | 1170 | ||
| 7656 | 10년 전 | 1582 | ||
| 7655 | 10년 전 | 1000 | ||
| 7654 |
개발짜증나
|
10년 전 | 863 | |
| 7653 |
네이비칼라
|
10년 전 | 885 | |
| 7652 |
밥먹고합시다
|
10년 전 | 815 | |
| 7651 |
플라이SINJI
|
10년 전 | 1514 | |
| 7650 |
개발짜증나
|
10년 전 | 1424 | |
| 7649 | 10년 전 | 455 | ||
| 7648 |
이미영ㅇㅇ
|
10년 전 | 872 | |
| 7647 | 10년 전 | 444 | ||
| 7646 | 10년 전 | 817 | ||
| 7645 | 10년 전 | 2327 | ||
| 7644 | 10년 전 | 825 | ||
| 7643 |
|
10년 전 | 2875 | |
| 7642 | 10년 전 | 1518 | ||
| 7641 | 10년 전 | 1144 | ||
| 7640 |
개발짜증나
|
10년 전 | 478 | |
| 7639 |
|
10년 전 | 818 | |
| 7638 |
개발짜증나
|
10년 전 | 1139 | |
| 7637 | 10년 전 | 1558 | ||
| 7636 | 10년 전 | 2917 | ||
| 7635 | 10년 전 | 1701 | ||
| 7634 | 10년 전 | 1890 | ||
| 7633 | 10년 전 | 2347 | ||
| 7632 | 10년 전 | 3956 | ||
| 7631 |
|
10년 전 | 1543 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기