0

我对如何为这种情况制作 sql 语句感到非常困惑。我一直在尝试,但没有运气,所以希望有人能提供帮助。我有 3 个表:表 PARENTS、表 CHILDREN 和表 SCHOOL

-PARENTS has
ID      name


-Children has
ID    parentID   nam

-SCHOOL has 

ID   parentID   chidrenID   schoolType

我想要的是让所有父母带着他们的孩子(无论他们是否上学)但是如果他们上学了,那么我需要 schoolType 是“highschool”

所以沿着这些思路(伪代码):

Select * from parents,children 
where parents.ID = Children.parentID and 
include school information 
where parents.ID = school.parentID and 
children.ID = school.childrenID and schoolType = "HighSchool"

有什么帮助???

4

2 回答 2

0
SELECT
  *
FROM PARENTS p
INNER JOIN CHILDREN c ON p.ID = c.parentID
LEFT JOIN SCHOOL s ON c.ID = s.childrenID AND 'HighSchool' = s.schoolType
于 2013-07-02T23:44:27.387 回答
0

我认为你需要一些这样的:

SELECT * from PARENTS P
         inner join CHILDREN C
             on P.ID=C.parentID
         left join SCHOOL S
            on  P.ID=S.parentID
            and C.ID=S.childrenID
WHERE S.schoolType is null
   OR S.schoolType = 'HighSchool'
于 2013-07-02T23:46:25.433 回答