【发布时间】:2013-04-26 23:07:22
【问题描述】:
我希望能够查询 MySQL 数据库,以仅根据父母和孩子查找一个人的兄弟姐妹。我们在这里假设一个人的父母的孩子,一个人的孩子的父母是那个人的兄弟姐妹。
人
id name
-----------
1 jonny
2 tom
3 sally
4 bill
5 katie
6 jessica
rels
id parent child
--------------------
1 2 3
2 5 6
3 2 1
4 1 6
5 1 4
6 6 2
我一直在试图弄清楚如何以一种可以给我这些结果的方式连接这两个表,但我做错了。
SELECT people.id,people.name FROM people, rels
INNER JOIN rels children ON people.id = rels.parent
INNER JOIN rels siblings ON children.id = siblings.child
WHERE children.child = '1'
【问题讨论】:
-
哈哈,抱歉 - 错字:p 打字时分心了 >_
标签: mysql sql join where-clause