-- 카테고리 테이블을 작성한다.
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년 전 | 1315 | ||
| 7729 | 10년 전 | 1173 | ||
| 7728 |
잘살아보자
|
10년 전 | 615 | |
| 7727 |
잘살아보자
|
10년 전 | 512 | |
| 7726 |
잘살아보자
|
10년 전 | 843 | |
| 7725 |
잘살아보자
|
10년 전 | 570 | |
| 7724 |
잘살아보자
|
10년 전 | 486 | |
| 7723 |
잘살아보자
|
10년 전 | 549 | |
| 7722 |
잘살아보자
|
10년 전 | 488 | |
| 7721 |
잘살아보자
|
10년 전 | 520 | |
| 7720 |
잘살아보자
|
10년 전 | 486 | |
| 7719 |
비긴어게인
|
10년 전 | 701 | |
| 7718 |
|
10년 전 | 2548 | |
| 7717 |
잘살아보자
|
10년 전 | 670 | |
| 7716 |
잘살아보자
|
10년 전 | 414 | |
| 7715 |
잘살아보자
|
10년 전 | 447 | |
| 7714 |
잘살아보자
|
10년 전 | 511 | |
| 7713 | 10년 전 | 1792 | ||
| 7712 | 10년 전 | 1730 | ||
| 7711 | 10년 전 | 1117 | ||
| 7710 | 10년 전 | 1409 | ||
| 7709 | 10년 전 | 1529 | ||
| 7708 | 10년 전 | 1470 | ||
| 7707 | 10년 전 | 866 | ||
| 7706 |
별지기천사
|
10년 전 | 581 | |
| 7705 | 10년 전 | 1085 | ||
| 7704 |
ICONdesignstudio
|
10년 전 | 640 | |
| 7703 | 10년 전 | 605 | ||
| 7702 |
|
10년 전 | 742 | |
| 7701 | 10년 전 | 1431 | ||
| 7700 | 10년 전 | 1116 | ||
| 7699 | 10년 전 | 586 | ||
| 7698 | 10년 전 | 1159 | ||
| 7697 | 10년 전 | 5183 | ||
| 7696 | 10년 전 | 672 | ||
| 7695 | 10년 전 | 1697 | ||
| 7694 | 10년 전 | 1082 | ||
| 7693 | 10년 전 | 1569 | ||
| 7692 | 10년 전 | 1312 | ||
| 7691 | 10년 전 | 836 | ||
| 7690 | 10년 전 | 1398 | ||
| 7689 | 10년 전 | 1028 | ||
| 7688 | 10년 전 | 634 | ||
| 7687 |
파랑새1597
|
10년 전 | 614 | |
| 7686 | 10년 전 | 864 | ||
| 7685 | 10년 전 | 1352 | ||
| 7684 | 10년 전 | 811 | ||
| 7683 | 10년 전 | 1123 | ||
| 7682 | 10년 전 | 1038 | ||
| 7681 | 10년 전 | 682 | ||
| 7680 | 10년 전 | 998 | ||
| 7679 | 10년 전 | 521 | ||
| 7678 | 10년 전 | 751 | ||
| 7677 | 10년 전 | 653 | ||
| 7676 |
|
10년 전 | 958 | |
| 7675 |
|
10년 전 | 1195 | |
| 7674 | 10년 전 | 1058 | ||
| 7673 | 10년 전 | 766 | ||
| 7672 | 10년 전 | 1107 | ||
| 7671 | 10년 전 | 916 | ||
| 7670 | 10년 전 | 684 | ||
| 7669 |
mashmellow
|
10년 전 | 1241 | |
| 7668 | 10년 전 | 730 | ||
| 7667 | 10년 전 | 1026 | ||
| 7666 |
senseme
|
10년 전 | 669 | |
| 7665 | 10년 전 | 523 | ||
| 7664 | 10년 전 | 1907 | ||
| 7663 |
mixx애교
|
10년 전 | 988 | |
| 7662 | 10년 전 | 1058 | ||
| 7661 |
hkhkah
|
10년 전 | 801 | |
| 7660 | 10년 전 | 1076 | ||
| 7659 |
커네드커네드
|
10년 전 | 946 | |
| 7658 |
바람돌이팡
|
10년 전 | 688 | |
| 7657 | 10년 전 | 1176 | ||
| 7656 | 10년 전 | 1587 | ||
| 7655 | 10년 전 | 1006 | ||
| 7654 |
개발짜증나
|
10년 전 | 868 | |
| 7653 |
네이비칼라
|
10년 전 | 893 | |
| 7652 |
밥먹고합시다
|
10년 전 | 819 | |
| 7651 |
플라이SINJI
|
10년 전 | 1523 | |
| 7650 |
개발짜증나
|
10년 전 | 1430 | |
| 7649 | 10년 전 | 461 | ||
| 7648 |
이미영ㅇㅇ
|
10년 전 | 883 | |
| 7647 | 10년 전 | 455 | ||
| 7646 | 10년 전 | 826 | ||
| 7645 | 10년 전 | 2335 | ||
| 7644 | 10년 전 | 830 | ||
| 7643 |
|
10년 전 | 2884 | |
| 7642 | 10년 전 | 1527 | ||
| 7641 | 10년 전 | 1150 | ||
| 7640 |
개발짜증나
|
10년 전 | 479 | |
| 7639 |
|
10년 전 | 823 | |
| 7638 |
개발짜증나
|
10년 전 | 1145 | |
| 7637 | 10년 전 | 1565 | ||
| 7636 | 10년 전 | 2921 | ||
| 7635 | 10년 전 | 1711 | ||
| 7634 | 10년 전 | 1899 | ||
| 7633 | 10년 전 | 2362 | ||
| 7632 | 10년 전 | 3968 | ||
| 7631 |
|
10년 전 | 1555 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기