-- 카테고리 테이블을 작성한다.
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;
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7930 | 9년 전 | 417 | ||
| 7929 | 9년 전 | 337 | ||
| 7928 | 9년 전 | 432 | ||
| 7927 | 9년 전 | 345 | ||
| 7926 | 9년 전 | 671 | ||
| 7925 | 9년 전 | 364 | ||
| 7924 | 9년 전 | 341 | ||
| 7923 | 9년 전 | 352 | ||
| 7922 | 9년 전 | 388 | ||
| 7921 | 9년 전 | 403 | ||
| 7920 | 9년 전 | 324 | ||
| 7919 | 9년 전 | 336 | ||
| 7918 | 9년 전 | 494 | ||
| 7917 | 9년 전 | 342 | ||
| 7916 | 9년 전 | 419 | ||
| 7915 | 9년 전 | 410 | ||
| 7914 | 9년 전 | 427 | ||
| 7913 | 9년 전 | 589 | ||
| 7912 | 9년 전 | 426 | ||
| 7911 | 9년 전 | 372 | ||
| 7910 | 9년 전 | 416 | ||
| 7909 | 9년 전 | 512 | ||
| 7908 | 9년 전 | 435 | ||
| 7907 | 9년 전 | 380 | ||
| 7906 | 9년 전 | 400 | ||
| 7905 | 9년 전 | 383 | ||
| 7904 | 9년 전 | 366 | ||
| 7903 | 9년 전 | 353 | ||
| 7902 | 9년 전 | 566 | ||
| 7901 |
|
9년 전 | 744 | |
| 7900 | 9년 전 | 598 | ||
| 7899 | 9년 전 | 394 | ||
| 7898 | 9년 전 | 396 | ||
| 7897 | 9년 전 | 353 | ||
| 7896 | 9년 전 | 366 | ||
| 7895 | 9년 전 | 473 | ||
| 7894 | 9년 전 | 397 | ||
| 7893 | 9년 전 | 352 | ||
| 7892 | 9년 전 | 406 | ||
| 7891 | 9년 전 | 772 | ||
| 7890 | 9년 전 | 1202 | ||
| 7889 | 9년 전 | 752 | ||
| 7888 |
limsy1987
|
9년 전 | 555 | |
| 7887 | 9년 전 | 557 | ||
| 7886 | 9년 전 | 454 | ||
| 7885 | 9년 전 | 419 | ||
| 7884 | 9년 전 | 419 | ||
| 7883 | 9년 전 | 416 | ||
| 7882 | 9년 전 | 469 | ||
| 7881 | 9년 전 | 451 | ||
| 7880 | 9년 전 | 586 | ||
| 7879 | 9년 전 | 472 | ||
| 7878 | 9년 전 | 1228 | ||
| 7877 | 9년 전 | 759 | ||
| 7876 | 9년 전 | 494 | ||
| 7875 | 9년 전 | 566 | ||
| 7874 |
|
9년 전 | 813 | |
| 7873 | 9년 전 | 525 | ||
| 7872 | 9년 전 | 686 | ||
| 7871 | 9년 전 | 491 | ||
| 7870 | 9년 전 | 614 | ||
| 7869 | 9년 전 | 439 | ||
| 7868 | 9년 전 | 460 | ||
| 7867 | 9년 전 | 435 | ||
| 7866 | 9년 전 | 501 | ||
| 7865 | 9년 전 | 461 | ||
| 7864 | 9년 전 | 519 | ||
| 7863 | 9년 전 | 519 | ||
| 7862 | 9년 전 | 471 | ||
| 7861 | 9년 전 | 649 | ||
| 7860 | 9년 전 | 634 | ||
| 7859 | 9년 전 | 421 | ||
| 7858 | 9년 전 | 708 | ||
| 7857 | 9년 전 | 1087 | ||
| 7856 | 9년 전 | 534 | ||
| 7855 | 9년 전 | 760 | ||
| 7854 | 9년 전 | 735 | ||
| 7853 | 9년 전 | 588 | ||
| 7852 | 9년 전 | 518 | ||
| 7851 | 9년 전 | 517 | ||
| 7850 | 9년 전 | 594 | ||
| 7849 | 9년 전 | 359 | ||
| 7848 | 9년 전 | 417 | ||
| 7847 | 9년 전 | 660 | ||
| 7846 | 9년 전 | 463 | ||
| 7845 | 9년 전 | 427 | ||
| 7844 | 9년 전 | 398 | ||
| 7843 | 9년 전 | 425 | ||
| 7842 | 9년 전 | 407 | ||
| 7841 | 9년 전 | 390 | ||
| 7840 | 9년 전 | 412 | ||
| 7839 | 9년 전 | 439 | ||
| 7838 | 9년 전 | 522 | ||
| 7837 | 9년 전 | 361 | ||
| 7836 | 9년 전 | 403 | ||
| 7835 | 9년 전 | 482 | ||
| 7834 |
|
9년 전 | 1198 | |
| 7833 | 9년 전 | 441 | ||
| 7832 | 9년 전 | 426 | ||
| 7831 | 9년 전 | 571 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기