作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这样的层次结构数据:
Id, Related
1, 0
2, 1
3, 1
4, 1
5, 4
6, 3
7, 0
8, 7
9, 3
10, 8
我想选择 2 个 parent ,然后为每个 parent 找到 2 个 child
结果:
id
1
2
3
7
8
类似于:
select * from TABLE T1 where T1.related=0 order by T2.id limit 2
union
select * from TABLE T2 where T1.id=T2.related order by T2.id limit 2
我一直在研究 mysql select 分层数据,但找不到任何可以帮助解决我的情况的东西。
最佳答案
您想要选择所有带有 Related = 0
的记录。然后您要随机选择最多两个 child 。一种解决方案是获取最小子 ID 和最大子 ID。您可以在子查询中查找 parent 。
select
related as parent_id,
min(id) as child1_id,
max(id) as child2_id
from mytable
where related in (select id from mytable where related = 0)
group by related;
如果您不想两次显示相同的子 ID(如果只有一个子 ID,即 min(id) = max(id)
),请将其更改为:
select
related as parent_id,
min(id) as child1_id,
nullif(max(id), min(id)) as child2_id
...
这些查询不会显示没有子项的 related = 0
记录,但好吧,没有子项,他们无论如何都不是父项:-)
关于MySQL 用联合选择层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50238782/
我正在尝试将多个水平链接的 Button 和 TextView 垂直链接为 View 集,但仍保持平面 View 层次结构。这是我的初始布局和代码:
到目前为止,我已经在Google BigQuery上训练了几种模型,目前我需要查看模型的外观(即架构,损失函数等)。 有没有办法获取这些信息? 最佳答案 仔细阅读文档后,我可以说该功能尚不存在。我什至
本文实例讲述了PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)。分享给大家供大家参考,具体如下: 前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个
我是一名优秀的程序员,十分优秀!