-- 카테고리 테이블을 작성한다.
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;
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8230 | 9년 전 | 51 | ||
| 8229 | 9년 전 | 52 | ||
| 8228 |
커네드커네드
|
9년 전 | 99 | |
| 8227 | 9년 전 | 108 | ||
| 8226 | 9년 전 | 145 | ||
| 8225 | 9년 전 | 134 | ||
| 8224 | 9년 전 | 132 | ||
| 8223 | 9년 전 | 92 | ||
| 8222 |
|
9년 전 | 162 | |
| 8221 | 9년 전 | 77 | ||
| 8220 | 9년 전 | 75 | ||
| 8219 | 9년 전 | 82 | ||
| 8218 | 9년 전 | 112 | ||
| 8217 |
star3840
|
9년 전 | 98 | |
| 8216 | 9년 전 | 135 | ||
| 8215 | 9년 전 | 89 | ||
| 8214 | 9년 전 | 208 | ||
| 8213 | 9년 전 | 142 | ||
| 8212 | 9년 전 | 57 | ||
| 8211 | 9년 전 | 222 | ||
| 8210 | 9년 전 | 220 | ||
| 8209 | 9년 전 | 319 | ||
| 8208 | 9년 전 | 188 | ||
| 8207 | 9년 전 | 201 | ||
| 8206 |
|
9년 전 | 165 | |
| 8205 | 9년 전 | 147 | ||
| 8204 | 9년 전 | 110 | ||
| 8203 | 9년 전 | 203 | ||
| 8202 | 9년 전 | 127 | ||
| 8201 | 9년 전 | 166 | ||
| 8200 | 9년 전 | 137 | ||
| 8199 | 9년 전 | 187 | ||
| 8198 | 9년 전 | 155 | ||
| 8197 | 9년 전 | 136 | ||
| 8196 | 9년 전 | 520 | ||
| 8195 | 9년 전 | 135 | ||
| 8194 | 9년 전 | 268 | ||
| 8193 | 9년 전 | 136 | ||
| 8192 | 9년 전 | 164 | ||
| 8191 | 9년 전 | 115 | ||
| 8190 | 9년 전 | 112 | ||
| 8189 | 9년 전 | 166 | ||
| 8188 | 9년 전 | 115 | ||
| 8187 | 9년 전 | 121 | ||
| 8186 | 9년 전 | 131 | ||
| 8185 | 9년 전 | 291 | ||
| 8184 | 9년 전 | 87 | ||
| 8183 | 9년 전 | 308 | ||
| 8182 | 9년 전 | 141 | ||
| 8181 | 9년 전 | 114 | ||
| 8180 | 9년 전 | 678 | ||
| 8179 | 9년 전 | 474 | ||
| 8178 | 9년 전 | 278 | ||
| 8177 |
kiplayer
|
9년 전 | 287 | |
| 8176 | 9년 전 | 330 | ||
| 8175 | 9년 전 | 204 | ||
| 8174 | 9년 전 | 211 | ||
| 8173 | 9년 전 | 324 | ||
| 8172 | 9년 전 | 170 | ||
| 8171 | 9년 전 | 161 | ||
| 8170 | 9년 전 | 280 | ||
| 8169 |
커네드커네드
|
9년 전 | 246 | |
| 8168 | 9년 전 | 299 | ||
| 8167 | 9년 전 | 305 | ||
| 8166 | 9년 전 | 218 | ||
| 8165 | 9년 전 | 148 | ||
| 8164 | 9년 전 | 280 | ||
| 8163 | 9년 전 | 266 | ||
| 8162 | 9년 전 | 274 | ||
| 8161 | 9년 전 | 272 | ||
| 8160 |
|
9년 전 | 468 | |
| 8159 | 9년 전 | 388 | ||
| 8158 | 9년 전 | 207 | ||
| 8157 | 9년 전 | 343 | ||
| 8156 | 9년 전 | 257 | ||
| 8155 | 9년 전 | 233 | ||
| 8154 |
00년생용띠
|
9년 전 | 578 | |
| 8153 | 9년 전 | 208 | ||
| 8152 |
|
9년 전 | 386 | |
| 8151 | 9년 전 | 387 | ||
| 8150 | 9년 전 | 479 | ||
| 8149 |
Jangfolk
|
9년 전 | 315 | |
| 8148 | 9년 전 | 149 | ||
| 8147 | 9년 전 | 358 | ||
| 8146 | 9년 전 | 412 | ||
| 8145 | 9년 전 | 344 | ||
| 8144 | 9년 전 | 310 | ||
| 8143 | 9년 전 | 165 | ||
| 8142 | 9년 전 | 411 | ||
| 8141 | 9년 전 | 359 | ||
| 8140 | 9년 전 | 908 | ||
| 8139 | 9년 전 | 233 | ||
| 8138 |
전갈자리남자
|
9년 전 | 376 | |
| 8137 | 9년 전 | 359 | ||
| 8136 | 9년 전 | 717 | ||
| 8135 |
|
9년 전 | 770 | |
| 8134 |
PlayPixel
|
9년 전 | 481 | |
| 8133 |
|
9년 전 | 420 | |
| 8132 | 9년 전 | 431 | ||
| 8131 | 9년 전 | 787 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기