-- 카테고리 테이블을 작성한다.
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;
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8030 | 9년 전 | 399 | ||
| 8029 | 9년 전 | 324 | ||
| 8028 | 9년 전 | 284 | ||
| 8027 | 9년 전 | 295 | ||
| 8026 | 9년 전 | 366 | ||
| 8025 | 9년 전 | 404 | ||
| 8024 | 9년 전 | 373 | ||
| 8023 | 9년 전 | 414 | ||
| 8022 | 9년 전 | 330 | ||
| 8021 | 9년 전 | 349 | ||
| 8020 | 9년 전 | 341 | ||
| 8019 | 9년 전 | 360 | ||
| 8018 | 9년 전 | 462 | ||
| 8017 | 9년 전 | 551 | ||
| 8016 | 9년 전 | 363 | ||
| 8015 | 9년 전 | 406 | ||
| 8014 | 9년 전 | 340 | ||
| 8013 | 9년 전 | 257 | ||
| 8012 | 9년 전 | 262 | ||
| 8011 | 9년 전 | 463 | ||
| 8010 | 9년 전 | 317 | ||
| 8009 | 9년 전 | 329 | ||
| 8008 | 9년 전 | 301 | ||
| 8007 | 9년 전 | 451 | ||
| 8006 | 9년 전 | 487 | ||
| 8005 |
|
9년 전 | 984 | |
| 8004 | 9년 전 | 373 | ||
| 8003 | 9년 전 | 443 | ||
| 8002 | 9년 전 | 338 | ||
| 8001 |
|
9년 전 | 682 | |
| 8000 | 9년 전 | 441 | ||
| 7999 | 9년 전 | 398 | ||
| 7998 | 9년 전 | 451 | ||
| 7997 | 9년 전 | 324 | ||
| 7996 | 9년 전 | 557 | ||
| 7995 | 9년 전 | 491 | ||
| 7994 | 9년 전 | 372 | ||
| 7993 | 9년 전 | 431 | ||
| 7992 | 9년 전 | 533 | ||
| 7991 | 9년 전 | 279 | ||
| 7990 | 9년 전 | 306 | ||
| 7989 | 9년 전 | 322 | ||
| 7988 | 9년 전 | 750 | ||
| 7987 | 9년 전 | 452 | ||
| 7986 | 9년 전 | 449 | ||
| 7985 | 9년 전 | 529 | ||
| 7984 | 9년 전 | 446 | ||
| 7983 | 9년 전 | 689 | ||
| 7982 | 9년 전 | 549 | ||
| 7981 | 9년 전 | 503 | ||
| 7980 | 9년 전 | 525 | ||
| 7979 | 9년 전 | 513 | ||
| 7978 | 9년 전 | 482 | ||
| 7977 | 9년 전 | 418 | ||
| 7976 | 9년 전 | 876 | ||
| 7975 | 9년 전 | 390 | ||
| 7974 | 9년 전 | 420 | ||
| 7973 | 9년 전 | 623 | ||
| 7972 | 9년 전 | 404 | ||
| 7971 | 9년 전 | 475 | ||
| 7970 | 9년 전 | 323 | ||
| 7969 | 9년 전 | 561 | ||
| 7968 | 9년 전 | 405 | ||
| 7967 | 9년 전 | 391 | ||
| 7966 | 9년 전 | 403 | ||
| 7965 |
|
9년 전 | 1037 | |
| 7964 | 9년 전 | 424 | ||
| 7963 | 9년 전 | 432 | ||
| 7962 | 9년 전 | 424 | ||
| 7961 |
전갈자리남자
|
9년 전 | 521 | |
| 7960 | 9년 전 | 987 | ||
| 7959 | 9년 전 | 567 | ||
| 7958 | 9년 전 | 427 | ||
| 7957 | 9년 전 | 381 | ||
| 7956 | 9년 전 | 380 | ||
| 7955 | 9년 전 | 480 | ||
| 7954 | 9년 전 | 410 | ||
| 7953 | 9년 전 | 457 | ||
| 7952 | 9년 전 | 381 | ||
| 7951 | 9년 전 | 517 | ||
| 7950 | 9년 전 | 413 | ||
| 7949 | 9년 전 | 408 | ||
| 7948 | 9년 전 | 343 | ||
| 7947 | 9년 전 | 954 | ||
| 7946 | 9년 전 | 467 | ||
| 7945 | 9년 전 | 420 | ||
| 7944 | 9년 전 | 465 | ||
| 7943 | 9년 전 | 404 | ||
| 7942 | 9년 전 | 424 | ||
| 7941 | 9년 전 | 415 | ||
| 7940 | 9년 전 | 917 | ||
| 7939 | 9년 전 | 391 | ||
| 7938 | 9년 전 | 421 | ||
| 7937 | 9년 전 | 306 | ||
| 7936 | 9년 전 | 901 | ||
| 7935 | 9년 전 | 482 | ||
| 7934 | 9년 전 | 456 | ||
| 7933 | 9년 전 | 571 | ||
| 7932 | 9년 전 | 522 | ||
| 7931 | 9년 전 | 575 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기