-- 카테고리 테이블을 작성한다.
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;
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7530 | 10년 전 | 818 | ||
| 7529 |
파랑새1597
|
10년 전 | 1242 | |
| 7528 |
파랑새1597
|
10년 전 | 1333 | |
| 7527 |
integrity7
|
10년 전 | 1414 | |
| 7526 | 10년 전 | 2441 | ||
| 7525 |
다빈치코드777
|
10년 전 | 1120 | |
| 7524 | 10년 전 | 1590 | ||
| 7523 | 10년 전 | 980 | ||
| 7522 |
|
10년 전 | 1008 | |
| 7521 |
blackkil
|
10년 전 | 1896 | |
| 7520 | 10년 전 | 1313 | ||
| 7519 |
Gaumi
|
10년 전 | 1093 | |
| 7518 | 10년 전 | 1504 | ||
| 7517 | 10년 전 | 843 | ||
| 7516 | 10년 전 | 1311 | ||
| 7515 | 10년 전 | 1429 | ||
| 7514 |
|
10년 전 | 4500 | |
| 7513 |
멋진남자임
|
10년 전 | 1140 | |
| 7512 |
다빈치코드777
|
10년 전 | 891 | |
| 7511 |
|
10년 전 | 3417 | |
| 7510 | 10년 전 | 1382 | ||
| 7509 | 10년 전 | 1162 | ||
| 7508 | 10년 전 | 726 | ||
| 7507 |
senseme
|
10년 전 | 768 | |
| 7506 |
멋진남자임
|
10년 전 | 1663 | |
| 7505 | 10년 전 | 4042 | ||
| 7504 | 10년 전 | 2171 | ||
| 7503 | 10년 전 | 1009 | ||
| 7502 | 10년 전 | 533 | ||
| 7501 | 10년 전 | 1462 | ||
| 7500 | 10년 전 | 1508 | ||
| 7499 | 10년 전 | 3412 | ||
| 7498 | 10년 전 | 1248 | ||
| 7497 |
dethos79
|
10년 전 | 2979 | |
| 7496 | 10년 전 | 2189 | ||
| 7495 | 10년 전 | 914 | ||
| 7494 |
CHAVO
|
10년 전 | 1156 | |
| 7493 | 10년 전 | 2668 | ||
| 7492 | 10년 전 | 1300 | ||
| 7491 | 10년 전 | 1509 | ||
| 7490 | 10년 전 | 2364 | ||
| 7489 | 10년 전 | 2132 | ||
| 7488 |
toptopon
|
10년 전 | 915 | |
| 7487 |
|
10년 전 | 1060 | |
| 7486 | 10년 전 | 3376 | ||
| 7485 | 10년 전 | 1331 | ||
| 7484 | 10년 전 | 1386 | ||
| 7483 | 10년 전 | 1044 | ||
| 7482 | 10년 전 | 677 | ||
| 7481 | 10년 전 | 868 | ||
| 7480 | 10년 전 | 1249 | ||
| 7479 | 10년 전 | 2622 | ||
| 7478 | 10년 전 | 1185 | ||
| 7477 |
멋진남자임
|
10년 전 | 1534 | |
| 7476 |
zeppeto
|
10년 전 | 1151 | |
| 7475 |
200점아빠
|
10년 전 | 932 | |
| 7474 | 10년 전 | 4020 | ||
| 7473 | 10년 전 | 1008 | ||
| 7472 |
나르시스1
|
10년 전 | 1257 | |
| 7471 | 10년 전 | 889 | ||
| 7470 | 10년 전 | 1307 | ||
| 7469 |
플라이SINJI
|
10년 전 | 1015 | |
| 7468 |
|
10년 전 | 576 | |
| 7467 |
|
10년 전 | 691 | |
| 7466 | 10년 전 | 1149 | ||
| 7465 | 10년 전 | 1201 | ||
| 7464 |
|
10년 전 | 1214 | |
| 7463 | 10년 전 | 1277 | ||
| 7462 |
진짜별사탕
|
10년 전 | 881 | |
| 7461 | 10년 전 | 968 | ||
| 7460 | 10년 전 | 3761 | ||
| 7459 |
멋진남자임
|
10년 전 | 1577 | |
| 7458 |
멋진남자임
|
10년 전 | 503 | |
| 7457 | 10년 전 | 933 | ||
| 7456 | 10년 전 | 783 | ||
| 7455 | 10년 전 | 2192 | ||
| 7454 | 10년 전 | 645 | ||
| 7453 | 10년 전 | 853 | ||
| 7452 |
중국어사이트제작
|
10년 전 | 516 | |
| 7451 | 10년 전 | 924 | ||
| 7450 | 10년 전 | 648 | ||
| 7449 |
울라라라우
|
10년 전 | 967 | |
| 7448 | 10년 전 | 1642 | ||
| 7447 |
멋진남자임
|
10년 전 | 525 | |
| 7446 | 10년 전 | 568 | ||
| 7445 |
네이비칼라
|
10년 전 | 1705 | |
| 7444 |
senseme
|
10년 전 | 1423 | |
| 7443 | 11년 전 | 1353 | ||
| 7442 | 11년 전 | 743 | ||
| 7441 |
멋진남자임
|
11년 전 | 1457 | |
| 7440 | 11년 전 | 930 | ||
| 7439 |
|
11년 전 | 786 | |
| 7438 |
|
11년 전 | 955 | |
| 7437 |
basement
|
11년 전 | 1050 | |
| 7436 |
잘살아보자
|
11년 전 | 1147 | |
| 7435 | 11년 전 | 1105 | ||
| 7434 | 11년 전 | 3801 | ||
| 7433 |
|
11년 전 | 2767 | |
| 7432 |
alexkim
|
11년 전 | 876 | |
| 7431 |
이웃집초보
|
11년 전 | 1325 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기