2011-10-09 2 views
1

콘텐츠 관리 시스템 용 콘텐츠 데이터를 저장하는 콘텐츠라는 mysql 테이블이 있습니다.MySQL 쿼리 - 데이터가 계층으로 구성되어있는 URL 이름을 사용하여 데이터 indentiyfing

참고 : 모든 콘텐츠는 상위 ID 열을 사용하여 계층으로 구성됩니다.

+----+------------+-----------------+--------+ 
| id | slug  | content_type_id | parent | 
+----+------------+-----------------+--------+ 
| 1 | portfolio |    5 |  0 | 
| 2 | about-us |    1 |  0 | 
| 3 | find-us |    1 |  0 | 
| 4 | contact-us |    1 |  2 | 
| 5 | find-us |    1 |  4 | 
+----+------------+-----------------+--------+ 

슬러그 이름이 무엇인지에 따라 테이블에서 올바른 행을 선택하는 쿼리가 필요합니다. 문제는 슬러그가 같은 이름을 가지고있을 때입니다.

나는 사용자가 방문 할 수있는 두 가지 경로가 있습니다입니다

가 가진 다른 열을 만들 : 나는 하나의 솔루션으로 생각할 수

/find-us/ 

/about-us/contact-us/find-us/ 

을 전체 경로 :

full_path 
-------- 
/portfolio/ 
/about-us/ 
/find-us/ 
/about-us/contact-us/ 
/about-us/contact-us/find-us/ 

그러나 올바른 행을 선택하는 데 사용할 수있는 영리한 방법이 있습니다. 전체 경로 이름을 가진 다른 열을 만드는 것이 좋은 아이디어인지는 모르겠지만 (개인적으로는이를 최후의 수단으로 사용하기를 바랄뿐입니다).

감사합니다.

+0

IIRC mysql을 허용하지 않는 재귀 쿼리를? – wildplasser

+1

전체 경로는 어쨌든 당신이 어떤 페이지를 올바르게 다루고 있는지를 알아야한다는 것입니다. 그래서 아마도 당신이 유지하기를 원하는 것 같아서 제 질문은 아마도 "유지하고 싶습니까?"하지만 "어떻게 유지할 수 있습니까?"라고 대답했습니다. 그것은 방아쇠를 통해입니다. – briantyler

+0

예 wildplasser, 올바른 재귀 쿼리가 불가능합니다. B 타일러 - 그건 내가 생각할 흥미로운 생각이고. – sidewinder

답변

-1

당신의 DBMS가 재귀 쿼리를 지원하는 경우이 가능할 것이다 :

DROP SCHEMA tmp CASCADE; 
CREATE SCHEMA tmp ; 

CREATE TABLE tmp.webmeuk 
    (id INTEGER NOT NULL PRIMARY KEY 
    , slug VARCHAR 
    , content_type_id INTEGER NOT NULL 
    , parent_id INTEGER REFERENCES tmp.webmeuk(id) 
    ); 
INSERT INTO tmp.webmeuk(id , slug, content_type_id , parent_id) 
VALUES( 0 , 'HTTP://pr0n.mysite.xx', 5 , NULL) 
    , ( 1 , 'portfolio', 5 , 0) 
    , ( 2 , 'about-us', 1 , 0) 
    , ( 3 , 'find-us',  1 , 0) 
    , ( 4 , 'contact-us', 1 , 2) 
    , ( 5 , 'find-us',  1 , 4) 
    ; 

-- a room with a view 
CREATE VIEW tmp.reteview AS (
    WITH RECURSIVE xx AS (
     SELECT w0.id AS id 
     , w0.slug AS slug 
     , w0.content_type_id AS content_type_id 
     , w0.slug AS fullpath 
     FROM tmp.webmeuk w0 
     WHERE w0.parent_id IS NULL 
     UNION 
     SELECT w1.id AS id 
     , w1.slug AS slug 
     , w1.content_type_id AS content_type_id 
     , xx.fullpath || '/'::text || w1.slug AS fullpath 
     FROM tmp.webmeuk w1, xx 
     WHERE w1.parent_id = xx.id 
     ) 
    SELECT * FROM xx 
    ); 

SELECT * FROM tmp.reteview ; 

-- Change one row of data 
UPDATE tmp.webmeuk 
SET slug = 'what-about-us' 
WHERE id = 2; 

SELECT * FROM tmp.reteview ; 

출력 :

NOTICE: drop cascades to 2 other objects 
DETAIL: drop cascades to table tmp.webmeuk 
drop cascades to view tmp.closure 
DROP SCHEMA 
CREATE SCHEMA 
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index "webmeuk_pkey" for table "webmeuk" 
CREATE TABLE 
INSERT 0 6 
CREATE VIEW 
id |   slug   | content_type_id |      fullpath     
----+-----------------------+-----------------+--------------------------------------------------- 
    0 | HTTP://pr0n.mysite.xx |    5 | HTTP://pr0n.mysite.xx 
    1 | portfolio    |    5 | HTTP://pr0n.mysite.xx/portfolio 
    2 | about-us    |    1 | HTTP://pr0n.mysite.xx/about-us 
    3 | find-us    |    1 | HTTP://pr0n.mysite.xx/find-us 
    4 | contact-us   |    1 | HTTP://pr0n.mysite.xx/about-us/contact-us 
    5 | find-us    |    1 | HTTP://pr0n.mysite.xx/about-us/contact-us/find-us 
(6 rows) 

UPDATE 1 
id |   slug   | content_type_id |      fullpath      
----+-----------------------+-----------------+-------------------------------------------------------- 
    0 | HTTP://pr0n.mysite.xx |    5 | HTTP://pr0n.mysite.xx 
    1 | portfolio    |    5 | HTTP://pr0n.mysite.xx/portfolio 
    3 | find-us    |    1 | HTTP://pr0n.mysite.xx/find-us 
    2 | what-about-us   |    1 | HTTP://pr0n.mysite.xx/what-about-us 
    4 | contact-us   |    1 | HTTP://pr0n.mysite.xx/what-about-us/contact-us 
    5 | find-us    |    1 | HTTP://pr0n.mysite.xx/what-about-us/contact-us/find-us 
(6 rows) 
관련 문제