-- 카테고리 테이블을 작성한다.
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;
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7630 | 10년 전 | 634 | ||
| 7629 |
|
10년 전 | 2339 | |
| 7628 | 10년 전 | 768 | ||
| 7627 |
|
10년 전 | 997 | |
| 7626 |
|
10년 전 | 1756 | |
| 7625 | 10년 전 | 656 | ||
| 7624 | 10년 전 | 681 | ||
| 7623 |
|
10년 전 | 3002 | |
| 7622 | 10년 전 | 684 | ||
| 7621 |
leeleeleelee
|
10년 전 | 565 | |
| 7620 | 10년 전 | 524 | ||
| 7619 | 10년 전 | 449 | ||
| 7618 | 10년 전 | 994 | ||
| 7617 | 10년 전 | 712 | ||
| 7616 | 10년 전 | 606 | ||
| 7615 | 10년 전 | 701 | ||
| 7614 | 10년 전 | 1207 | ||
| 7613 |
|
10년 전 | 2047 | |
| 7612 | 10년 전 | 1117 | ||
| 7611 | 10년 전 | 1380 | ||
| 7610 |
|
10년 전 | 1872 | |
| 7609 |
|
10년 전 | 1288 | |
| 7608 |
mwdkim
|
10년 전 | 1092 | |
| 7607 |
|
10년 전 | 1020 | |
| 7606 |
mwdkim
|
10년 전 | 3897 | |
| 7605 | 10년 전 | 664 | ||
| 7604 | 10년 전 | 1002 | ||
| 7603 | 10년 전 | 1626 | ||
| 7602 |
|
10년 전 | 1041 | |
| 7601 |
AniNest
|
10년 전 | 2758 | |
| 7600 |
port443
|
10년 전 | 991 | |
| 7599 | 10년 전 | 915 | ||
| 7598 | 10년 전 | 988 | ||
| 7597 | 10년 전 | 4550 | ||
| 7596 |
SeungYeon
|
10년 전 | 865 | |
| 7595 |
untitled
|
10년 전 | 2386 | |
| 7594 |
프로그래머7
|
10년 전 | 1700 | |
| 7593 |
untitled
|
10년 전 | 2337 | |
| 7592 |
untitled
|
10년 전 | 1908 | |
| 7591 |
untitled
|
10년 전 | 2647 | |
| 7590 |
아리마2001
|
10년 전 | 820 | |
| 7589 | 10년 전 | 1078 | ||
| 7588 |
|
10년 전 | 2894 | |
| 7587 | 10년 전 | 1272 | ||
| 7586 | 10년 전 | 631 | ||
| 7585 | 10년 전 | 1655 | ||
| 7584 | 10년 전 | 1388 | ||
| 7583 |
leeleeleelee
|
10년 전 | 1123 | |
| 7582 |
|
10년 전 | 1070 | |
| 7581 | 10년 전 | 1302 | ||
| 7580 | 10년 전 | 942 | ||
| 7579 |
|
10년 전 | 579 | |
| 7578 | 10년 전 | 1387 | ||
| 7577 |
|
10년 전 | 1850 | |
| 7576 | 10년 전 | 1365 | ||
| 7575 |
멋진남자임
|
10년 전 | 1445 | |
| 7574 | 10년 전 | 2084 | ||
| 7573 | 10년 전 | 3212 | ||
| 7572 | 10년 전 | 745 | ||
| 7571 |
|
10년 전 | 770 | |
| 7570 |
|
10년 전 | 1286 | |
| 7569 | 10년 전 | 1519 | ||
| 7568 |
this1mg
|
10년 전 | 1021 | |
| 7567 |
|
10년 전 | 729 | |
| 7566 | 10년 전 | 888 | ||
| 7565 |
Angel하늘
|
10년 전 | 962 | |
| 7564 |
seoldi
|
10년 전 | 1195 | |
| 7563 |
|
10년 전 | 1345 | |
| 7562 |
멋진남자임
|
10년 전 | 2044 | |
| 7561 | 10년 전 | 679 | ||
| 7560 |
leeleeleelee
|
10년 전 | 877 | |
| 7559 | 10년 전 | 5008 | ||
| 7558 |
RinaP
|
10년 전 | 753 | |
| 7557 |
|
10년 전 | 1217 | |
| 7556 | 10년 전 | 1168 | ||
| 7555 |
hyohyojj1234
|
10년 전 | 1633 | |
| 7554 | 10년 전 | 1068 | ||
| 7553 |
senseme
|
10년 전 | 1321 | |
| 7552 |
ehdltdoit
|
10년 전 | 1413 | |
| 7551 |
|
10년 전 | 1794 | |
| 7550 |
leeleeleelee
|
10년 전 | 1558 | |
| 7549 | 10년 전 | 2393 | ||
| 7548 | 10년 전 | 1812 | ||
| 7547 |
멋진남자임
|
10년 전 | 1921 | |
| 7546 | 10년 전 | 964 | ||
| 7545 |
ILMare1003
|
10년 전 | 1255 | |
| 7544 |
|
10년 전 | 1211 | |
| 7543 | 10년 전 | 860 | ||
| 7542 | 10년 전 | 635 | ||
| 7541 |
울라라라우
|
10년 전 | 846 | |
| 7540 | 10년 전 | 1578 | ||
| 7539 | 10년 전 | 897 | ||
| 7538 |
|
10년 전 | 1807 | |
| 7537 | 10년 전 | 3587 | ||
| 7536 |
Gaumi
|
10년 전 | 1379 | |
| 7535 |
프로그램은어려워
|
10년 전 | 1241 | |
| 7534 |
senseme
|
10년 전 | 1191 | |
| 7533 | 10년 전 | 1165 | ||
| 7532 | 10년 전 | 833 | ||
| 7531 | 10년 전 | 2025 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기