mysql - 带有 groupBy 的 MYSQL 子查询并具有 - 检查“child”是否有“cousins”,如果有则过滤掉
问题描述
这是示例数据库,您可以在问题末尾的 mysql fiddle 中看到更详细的数据库:
children:
id, name, age, date_of_birth, favorite_book, parent_id
parents:
id, gender, grandparent_id (nullable)
grandparents:
id
我想构建以下查询:
返回所有最喜欢的书,按孩子的出生日期排序,没有祖父母或阿姨的表兄弟姐妹。
这是一个愚蠢的例子:),我知道。
这可以在一个查询中完成吗?
这些方面的东西:
SELECT
children.id,
children.name,
children.date_of_birth,
children.favorite_book,
hasYoungerAuntCousins <-- this is what I don't know how to set up
FROM
children
ORDER BY
children.date_of_birth DESC
GROUP BY
favorite_book
HAVING
hasYoungerAuntCousins == 0
所以结果会是这样的:
2, Joe, 1/1/2010, Basics 3 of MySQL, 0
9, Moe, 1/1/2005, Basics 1 of MySQL, 0
5, Zoe, 1/1/2001, Basics 2 of MySQL, 0
...
hasYoungerAuntCousins
子查询应0
在两种情况下返回:
- 如果列
parent
有 NULLgrandparent_id
- 如果
parent
有grandparent
和祖父母有女儿(女性) - 孩子的阿姨 - 有比child
我们“迭代”更年轻的孩子。
让我知道这是否有意义以及我是否遗漏了什么。
与此同时,我正在寻找解决方案,也感谢评论中的任何指针。
编辑:
这是一个小提琴链接:
解决方案
推荐阅读
- python - Python,子级无法从父级继承函数
- python - 为什么我的 sklearn 自定义转换器在 ColumnTransformer 中使用时不保存属性?
- c - 49个线程的线程屏障
- google-cloud-platform - 我如何搜索用户以外的内容?
- javascript - 如何复制 reddit 的帖子视图,其中单击帖子以模式打开并更改 URL 或通过直接访问 URL 在整页中打开?
- python - 使用 Python/Pandas 创建包含多张工作表的 Excel 文件
- reactjs - reactjs如何显示图表
- c# - 用于 wpf 应用程序的 Visual c++ 本地部署(cef sharp 需要)
- c++ - 如何反转最后 Y 位并获得正数?
- image - 如何通过单击图像输出 x,y 坐标?