联合和子查询(MySQL)
我甚至不确定标题是否正确,但情况是这样的,简化了 我有一个表,其中包含邻接列表:联合和子查询(MySQL),mysql,sql,Mysql,Sql,我甚至不确定标题是否正确,但情况是这样的,简化了 我有一个表,其中包含邻接列表: comments - id (int) - parent_id (int) - depth_level (int) - ... 我要做的是查询具有order by和limit的深度级别0,对于返回的每一行,我需要一个union,该union具有一个查询,该查询返回相同的表和order by和limit,但深度级别不同,我希望该子查询只返回与父深度级别相关的行。。。等等如果有帮助的话,我可以限制深度。 我被困在没有
comments
- id (int)
- parent_id (int)
- depth_level (int)
- ...
我要做的是查询具有order by和limit的深度级别0,对于返回的每一行,我需要一个union,该union具有一个查询,该查询返回相同的表和order by和limit,但深度级别不同,我希望该子查询只返回与父深度级别相关的行。。。等等如果有帮助的话,我可以限制深度。
我被困在没有参考资料的地方,就像这样:
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只包含父id为D0 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
但我想要的是对每个父级的深度级别进行限制,而不是对深度级别的总数进行限制。如下所示(本例中每深度1 5个):
这很难看,但你能做到
(
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 |
|ID |父项| ID |深度|标题|
|----|-----------|-------|----------|
|1 | 0 | 0 |标题1|
|3 | 1 | 1 |标题3|
|4 | 1 | 1 |标题4|
|8 | 4 | 2 |标题8|
|9 | 4 | 2 |标题9|
|10 | 4 | 2 |标题10|
这里是演示你能展示一些样本数据吗?你想要的输出是什么样子的?@Tom我试着提供一个例子,不确定这是否会清除内容。这实际上只从第一个深度=0级别获取深度级别>0。对于其他深度=0行,没有深度1,2,3。。。(我需要6个)。虽然它给了我一些想法,但我还是不确定。谢谢你的努力!编辑:进一步限制深度没有显示其余的查询,它们位于深度=0的级别。但是,限制仅限于每个深度的总计,而不是每个父行。例如,如果depth=1有限制2,那么我不希望depth=1的总限制2,而是希望返回的行的perdepth=0。 | 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 |