我有孩子,妈妈M:M Table.How选择孩子与他们有共同的母亲?

时间:2018-01-20 08:50:25

标签: sql-server vb.net many-to-many

我有3张表,包括母亲,儿童和Junction这样的

enter image description here

Child(ChildName ZZZ应该有53个代码)

enter image description here

交界处

enter image description here

我想用母亲(至少2个)作为条件来了解他们的共同点 像

 Select CodeChild 
    From Junction
    Where CodeMother in ('02','03')

我期望得到的结果

enter image description here

2 个答案:

答案 0 :(得分:0)

无法测试此

Select Junction.CodeChild, Child.ChildName From Junction
Inner Join Child On Junction.ChildCode = Child.CodeChild
Where Junction.CodeMother = CodeMother

答案 1 :(得分:0)

根据OP编辑 解决但欣赏任何更好的方法 由于母亲条件可以有1-4,查询取决于那个

例如

Select r.ChildName
From (Select j.CodeChild From Junct j Where j.CodeMother = 2)M1
inner join (Select j.CodeChild From Junct j Where j.CodeMother =    3)M2 On
M2.CodeChild = M1.CodeChild
//If More Mother Condition Just put inner join M3,M4 and So on Here
inner join Child r on M1.CodeChild = r.CodeChild