2012-07-28 28 views
1

我在MYSQL中爲某些QA論壇(例如。stackoverflow)提供了一個表格「posts」和下面的示例數據。如何在mySQL中使用「CTE」?

INSERT INTO POSTS (postID, type, parentID, QuesTitle) VALUES(1,'Q',NULL,'sometext'); 
    INSERT INTO POSTS (postID, type, parentID, QuesTitle) VALUES(2,'Q',NULL,'randomtext'); 
    INSERT INTO POSTS (postID, type, parentID, QuesTitle) VALUES(3,'A',1,NULL); 
    INSERT INTO POSTS (postID, type, parentID, QuesTitle) VALUES(4,'A',1,NULL); 
    INSERT INTO POSTS (postID, type, parentID, QuesTitle) VALUES(5,'Q',NULL,'titletext'); 
    INSERT INTO POSTS (postID, type, parentID, QuesTitle) VALUES(6,'A',1,NULL); 
    INSERT INTO POSTS (postID, type, parentID, QuesTitle) VALUES(7,'A',2,NULL); 
    INSERT INTO POSTS (postID, type, parentID, QuesTitle) VALUES(8,'A',2,NULL); 

postID是主鍵,parentID引用postID。一個「Q」(問題)類型的帖子可以有多個「A」(答案)帖子(由他們的parentID顯示)。只有「Q」的帖子有標題,但是「A」的帖子沒有標題(NULL)

我想選擇「A」類型的帖子及其帖子ID,它們的父ID和它所指的Questitle。例如,要求的結果是 -

postID parentID QuesTitle 
    3   1   sometext 
    4   1   sometext 
    6   1   sometext 
    7   2   randomtext 
    8   2   randomtext 

使用'with'子句CTE會更容易,但MYSQL不支持它。有人可以幫助我在MYSQL中實現它嗎?

+0

你不這樣做,MySQL不支持的CTE。你需要一個閉合表。 – 2015-08-13 14:25:45

回答

1

爲什麼你需要一個CTE?

你可以簡單地做一個自聯接,以獲得parentID的信息:

SELECT a.postID, a.parentID, b.QuesTitle 
FROM posts a 
JOIN posts b ON a.parentID = b.postID 
WHERE a.type = 'A' 
4

在他們的榜樣,可能不是很清楚,爲什麼他們會想要使用一個CTE,但我同意如果MySQL會添加這個非常缺乏的功能(提示提示MariaDB基礎),那將是非常好的。

CTE對於任意深度層次數據可能非常有用。

這裏是Postgres例如:

CREATE TABLE posts (
    postid integer, 
    type character varying(100), 
    parentid integer, 
    questitle character varying(100) 
); 
INSERT INTO posts VALUES (1, 'Q', NULL, 'sometext'); 
INSERT INTO posts VALUES (2, 'Q', NULL, 'randomtext'); 
INSERT INTO posts VALUES (3, 'A', 1, NULL); 
INSERT INTO posts VALUES (4, 'A', 1, NULL); 
INSERT INTO posts VALUES (5, 'Q', NULL, 'titletext'); 
INSERT INTO posts VALUES (6, 'A', 1, NULL); 
INSERT INTO posts VALUES (7, 'A', 2, NULL); 
INSERT INTO posts VALUES (8, 'A', 2, NULL); 
INSERT INTO posts VALUES (9, 'A', 8, NULL); 
INSERT INTO posts VALUES (10, 'A', 4, NULL); 
INSERT INTO posts VALUES (11, 'A', 10, NULL); 
INSERT INTO posts VALUES (12, 'A', 11, NULL); 

用下面的查詢:

with recursive posts_cte (postid, type, parentid, questitle,level,id_lineage) as 
(
    -- base case 
    select 
     postid, type, parentid, questitle, 0 as level, ''||postid as id_lineage 
    from 
     POSTS 
    where 
     parentid is null 

    UNION ALL 

    -- recursion case 
    select 
     c.postid, c.type, c.parentid, c.questitle, p.level+1 as level, p.id_lineage||','||c.postid as id_lineage 
    from 
     posts c 
     inner join posts_cte p on 
      c.parentid = p.postid 
) 
select 
    postid, type, parentid, questitle,level,id_lineage 
from 
    posts_cte 
order by 
    id_lineage; 

生成結果集:

postid | type | parentid | questitle | level | id_lineage 
--------+------+----------+------------+-------+-------------- 
     1 | Q |   | sometext |  0 | 1 
     3 | A |  1 |   |  1 | 1,3 
     4 | A |  1 |   |  1 | 1,4 
    10 | A |  4 |   |  2 | 1,4,10 
    11 | A |  10 |   |  3 | 1,4,10,11 
    12 | A |  11 |   |  4 | 1,4,10,11,12 
     6 | A |  1 |   |  1 | 1,6 
     2 | Q |   | randomtext |  0 | 2 
     7 | A |  2 |   |  1 | 2,7 
     8 | A |  2 |   |  1 | 2,8 
     9 | A |  8 |   |  2 | 2,8,9 
     5 | Q |   | titletext |  0 | 5