sql - 简单的家谱查询
问题描述
只有一张桌子。
表名 Family_tree。
查询尼克的所有孩子。尼克也可以当妈妈!
NAME ID FATHER_ID MOTHER_ID
--------------- ---------- ---------- ----------
Nick 23 25 24
Jane 10 27 26
Perl 15 9 13
Katrin 50 6 12
Sandra 1 3 8
Demi 2 3 8
Deimar 3 7 5
Gandalf 4 6 5
Bill 5 10 23
Kelly 6 22 43
Dolmar 7 11 20
解决方案
可能是这样的:
select name, father_id, mother_id from family_tree
where father_id in
(select id from family_tree
where name = 'Nick')
or mother_id in
(select id from family_tree
where name = 'Nick')
或者如果您只需要尼克作为父亲,那么:
select name, father_id, mother_id from family_tree
where father_id in
(select id from family_tree
where name = 'Nick')
此外,如果您只需要 children ,则不需要使用connect by...prior用于孙子、孙子等。
推荐阅读
- css - 如何在 App.css 的反应中从公共文件夹中导入图像
- javascript - 如何更改 index.html(firebase 托管)以显示项目内容?
- discord.js - 如何解决此错误“无法设置未定义的属性”
- python - Flask 从带有文本区域的表单输入数据
- javascript - 存储新的选项标签时如何在选择标签中删除以前存储的选项标签
- xamarin - 如何在 xamarin 中防止 android 和 ios 字体设置为粗体/更大的字体大小?
- sqlite - 在 SQLite 上获取具有 JOINed 值的数组
- python - 改进以逗号分隔和爆炸字符串列,并在 Pandas 中将其从长旋转到宽
- javascript - 如何将 For 循环转换为 Reduce 检查名称和求和行?
- python - 在 websocket 消息事件循环中返回 asyncio.create_task() 的值