-- 카테고리 테이블을 작성한다.
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년 전 | 179 | ||
| 8229 | 9년 전 | 160 | ||
| 8228 |
커네드커네드
|
9년 전 | 199 | |
| 8227 | 9년 전 | 243 | ||
| 8226 | 9년 전 | 259 | ||
| 8225 | 9년 전 | 238 | ||
| 8224 | 9년 전 | 249 | ||
| 8223 | 9년 전 | 220 | ||
| 8222 |
|
9년 전 | 284 | |
| 8221 | 9년 전 | 181 | ||
| 8220 | 9년 전 | 226 | ||
| 8219 | 9년 전 | 196 | ||
| 8218 | 9년 전 | 243 | ||
| 8217 |
star3840
|
9년 전 | 209 | |
| 8216 | 9년 전 | 274 | ||
| 8215 | 9년 전 | 220 | ||
| 8214 | 9년 전 | 329 | ||
| 8213 | 9년 전 | 288 | ||
| 8212 | 9년 전 | 201 | ||
| 8211 | 9년 전 | 368 | ||
| 8210 | 9년 전 | 368 | ||
| 8209 | 9년 전 | 445 | ||
| 8208 | 9년 전 | 335 | ||
| 8207 | 9년 전 | 343 | ||
| 8206 |
|
9년 전 | 289 | |
| 8205 | 9년 전 | 262 | ||
| 8204 | 9년 전 | 253 | ||
| 8203 | 9년 전 | 328 | ||
| 8202 | 9년 전 | 243 | ||
| 8201 | 9년 전 | 280 | ||
| 8200 | 9년 전 | 289 | ||
| 8199 | 9년 전 | 310 | ||
| 8198 | 9년 전 | 275 | ||
| 8197 | 9년 전 | 256 | ||
| 8196 | 9년 전 | 681 | ||
| 8195 | 9년 전 | 268 | ||
| 8194 | 9년 전 | 378 | ||
| 8193 | 9년 전 | 291 | ||
| 8192 | 9년 전 | 303 | ||
| 8191 | 9년 전 | 257 | ||
| 8190 | 9년 전 | 242 | ||
| 8189 | 9년 전 | 298 | ||
| 8188 | 9년 전 | 237 | ||
| 8187 | 9년 전 | 246 | ||
| 8186 | 9년 전 | 246 | ||
| 8185 | 9년 전 | 415 | ||
| 8184 | 9년 전 | 202 | ||
| 8183 | 9년 전 | 410 | ||
| 8182 | 9년 전 | 281 | ||
| 8181 | 9년 전 | 238 | ||
| 8180 | 9년 전 | 804 | ||
| 8179 | 9년 전 | 586 | ||
| 8178 | 9년 전 | 446 | ||
| 8177 |
kiplayer
|
9년 전 | 438 | |
| 8176 | 9년 전 | 471 | ||
| 8175 | 9년 전 | 360 | ||
| 8174 | 9년 전 | 352 | ||
| 8173 | 9년 전 | 439 | ||
| 8172 | 9년 전 | 322 | ||
| 8171 | 9년 전 | 279 | ||
| 8170 | 9년 전 | 404 | ||
| 8169 |
커네드커네드
|
9년 전 | 358 | |
| 8168 | 9년 전 | 437 | ||
| 8167 | 9년 전 | 430 | ||
| 8166 | 9년 전 | 325 | ||
| 8165 | 9년 전 | 267 | ||
| 8164 | 9년 전 | 403 | ||
| 8163 | 9년 전 | 408 | ||
| 8162 | 9년 전 | 393 | ||
| 8161 | 9년 전 | 407 | ||
| 8160 |
|
9년 전 | 629 | |
| 8159 | 9년 전 | 571 | ||
| 8158 | 9년 전 | 368 | ||
| 8157 | 9년 전 | 480 | ||
| 8156 | 9년 전 | 356 | ||
| 8155 | 9년 전 | 364 | ||
| 8154 |
00년생용띠
|
9년 전 | 696 | |
| 8153 | 9년 전 | 334 | ||
| 8152 |
|
9년 전 | 514 | |
| 8151 | 9년 전 | 508 | ||
| 8150 | 9년 전 | 624 | ||
| 8149 |
Jangfolk
|
9년 전 | 479 | |
| 8148 | 9년 전 | 296 | ||
| 8147 | 9년 전 | 478 | ||
| 8146 | 9년 전 | 559 | ||
| 8145 | 9년 전 | 518 | ||
| 8144 | 9년 전 | 488 | ||
| 8143 | 9년 전 | 314 | ||
| 8142 | 9년 전 | 527 | ||
| 8141 | 9년 전 | 471 | ||
| 8140 | 9년 전 | 1037 | ||
| 8139 | 9년 전 | 383 | ||
| 8138 |
전갈자리남자
|
9년 전 | 490 | |
| 8137 | 9년 전 | 524 | ||
| 8136 | 9년 전 | 855 | ||
| 8135 |
|
9년 전 | 896 | |
| 8134 |
PlayPixel
|
9년 전 | 636 | |
| 8133 |
|
9년 전 | 543 | |
| 8132 | 9년 전 | 581 | ||
| 8131 | 9년 전 | 941 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기