2013-10-01 131 views
0

我甚至不確定標題是否正確,但這裏是簡單的情況。UNION和子查詢(MySQL)

我有鄰接表一個表吧:

comments 
- id (int) 
- parent_id (int) 
- depth_level (int) 
- ... 

我想要做的是和限制以及每個行與以查詢深度級別返回0我想和工會一個查詢返回相同的表和一個順序,但是限制但不同的深度級別,我希望子查詢只返回父深度級別相關的行......等等。如果有幫助,我可以限制深度級別。 我有點卡住,沒有參考,像這樣:

select * from (select * from comments where depth = 0 order by id asc LIMIT 10) D0 
union all 
select * from (select * from comments where depth = 1 order by id asc LIMIT 10) D1 

我得到聯合在一起行,但你可以看到,我想D1僅包含已與D0 IDS PARENT_ID行...和我想要多個層面。也許這是做錯的方法。我知道這是一廂情願的想法,但如果我能得到每一行,如果有更多的行比提供的限制,這將是很好的。

一個例子:

id parent_id depth title 
1 0   0  Title 1 
2 0   0  Title 2 
3 1   1  Title 3 
4 1   1  Title 4 
5 1   1  Title 5 
6 1   1  Title 6 
7 1   1  Title 7 
8 4   2  Title 8 
9 4   2  Title 9 
10 4   2  Title 10 
11 4   2  Title 11 

pseudo: 
select * from table where depth = 0 order by id asc limit 1 
union 
select * from table where depth = 1 and parent_id from firstQuery.id order by id asc limit 2 
union 
select * from table where depth = 2 and parent_id from secondQuery.id order by id asc limit 3 

result: 

id parent_id depth title 
1 0   0  Title 1 
3 1   1  Title 3 
4 1   1  Title 4 
8 4   2  Title 8 
9 4   2  Title 9 
10 4   2  Title 10 

編輯2:

要在peterm的答案擴大。

(
SELECT * 
FROM comments 
WHERE depth = 0 
ORDER BY id DESC 
LIMIT 2 
) 
UNION ALL 
(
    SELECT c.* 
    FROM comments c JOIN 
    (
     SELECT id 
     FROM comments 
     WHERE depth = 0 
     ORDER BY id DESC 
     LIMIT 2 
    ) p ON c.parent_id = p.id 
    LIMIT 5 
) 


id parent_id depth title 
1 0   0  Title 1 
2 0   0  Title 2 
3 1   1  Title 3 
4 1   1  Title 4 
5 1   1  Title 5 
6 1   1  Title 6 
7 1   1  Title 7 

但我想要的是限制PER父級深度級別,而不是深度級別的總和限制。像這樣(5%在這個例子中深度1):

id parent_id depth title 
1 0   0  Title 1 
2 0   0  Title 2 
3 1   1  Title 3 
4 1   1  Title 4 
5 1   1  Title 5 
6 1   1  Title 6 
7 1   1  Title 7 
8 2   1  Title 8 
9 2   1  Title 9 
10 2   1  Title 10 
11 2   1  Title 11 
12 2   1  Title 12 
+1

你能否顯示一些樣本數據,以及你想要的輸出是什麼樣的? – Tom

+0

@Tom我試圖提供一個例子,不知道是否清除了東西。 – Keyframe

回答

1

這是醜陋的,但你可以做

(
    SELECT * 
    FROM comments 
    WHERE depth = 0 
    ORDER BY id 
    LIMIT 1 
) 
UNION ALL 
(
    SELECT c.* 
    FROM comments c JOIN 
    (
    SELECT id 
    FROM comments 
    WHERE depth = 0 
    ORDER BY id 
    LIMIT 1 
) p ON c.parent_id = p.id 
    LIMIT 2 
) 
UNION ALL 
(
    SELECT c.* 
    FROM comments c JOIN 
    (
    SELECT c.* 
    FROM comments c JOIN 
    (
     SELECT id 
     FROM comments 
     WHERE depth = 0 
     ORDER BY id 
     LIMIT 1 
    ) q ON c.parent_id = q.id 
    LIMIT 2 
) p ON c.parent_id = p.id 
    LIMIT 3 
) 
-- ORDER BY id 

輸出:

 
| ID | PARENT_ID | DEPTH | TITLE | 
|----|-----------|-------|----------| 
| 1 |   0 |  0 | Title 1 | 
| 3 |   1 |  1 | Title 3 | 
| 4 |   1 |  1 | Title 4 | 
| 8 |   4 |  2 | Title 8 | 
| 9 |   4 |  2 | Title 9 | 
| 10 |   4 |  2 | Title 10 | 

這裏是SQLFiddle演示

+0

這實際上僅從第一個深度= 0級別獲取深度級別> 0。對於其他深度= 0行沒有深度1,2,3 ...(我需要6)。雖然它給了我很少的想法,但我仍然不確定。謝謝你的努力!編輯:進一步限制深度沒有顯示其餘的查詢,他們在深度= 0的水平。但是,限制僅限於每個深度,而不是每個父行。例如,如果depth = 1的限制爲2,那麼我不希望depth = 1的總限制2,但是每個depth = 0的返回行。 – Keyframe