从两个表语句中选择

时间:2014-09-18 21:00:16

标签: database vb.net ms-access ms-access-2010

我有两张桌子

+------------------------------------+
| Family                             |
|------------------------------------|
| FamilyID                           |
| Family Tel                         |
| Address                            |
| Area                               |
+------------------------------------+

+------------------------------------+
| Members                            |
|------------------------------------|
| Family ID                          |
| Person Name                        |
| Person Tel                         | 
| Person Job                         |
| Birthday                           |
+------------------------------------+

我想在列members中的表Person Name中进行搜索,但我希望它返回表中具有相同Family ID的所有成员,并且还要返回表Family表格{{1}}相同ID的家庭数据,尽管搜索将按人名

有任何线索吗?

2 个答案:

答案 0 :(得分:0)

因为只有一个家庭和许多成员,所以最好做两个查询而不是一个(一个查询,每个成员都有自己的家庭信息副本)。

SELECT Family.* from Family INNER JOIN Members ON Members.FamilyID = Family.Family
WHERE Member.PersonName = "Joe"

SELECT B.* from Members A INNER JOIN Members B ON A.FamilyID = B.FamilyID
WHERE A.PersonName = "Joe"

如果你真的想要一个查询:

SELECT B.*, Family.* FROM Members A INNER JOIN Members B ON A.FamilyID = B.FamilyID, Family
WHERE A.PersonName = "Joe"
AND Family.FamilyID = B.FamilyID

答案 1 :(得分:0)

诀窍是加入成员表一次,按名称过滤一个porson,一次显示所有家庭成员 尝试一下这方面的事情:

    SELECT members.*, family.adress
    FROM members AS members_1 INNER JOIN (members INNER JOIN family ON     members.familyId=family.FamilyId) ON members_1.familyId = members.familyId
    WHERE (((members_1.name)="john"));