UNION和子查询(MySQL)

时间:2013-10-01 00:05:32

标签: mysql sql

我甚至不确定标题是否正确,但这是简化的情况。

我有一个包含邻接列表的表:

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 id的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个):

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 个答案:

答案 0 :(得分: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 演示