一个表中的多个外键链接到第二个表中的单个主键

发布于 2024-10-09 15:41:25 字数 316 浏览 10 评论 0原文

我有一个包含三个表的数据库,一个家庭表,一个成人表和一个用户表。 Household 表包含两个外键:iAdult1ID 和 iAdult2ID。 Users 表有一个 iUserID 主键,Adult 表有一个相应的 iUserID 外键。 Users 表中的一列是 strUsername,即电子邮件地址。

我正在尝试编写一个查询,以便搜索与该家庭有关系的任何成年人的电子邮件地址。所以我有两个问题,假设所有值都不为空,我该怎么做?

第二,实际上,iAdult2ID 可以为 null,是否仍然可以编写查询来执行此操作?

感谢您的帮助。如果您需要更多信息,请告诉我。

I have a database with three tables, a household table, an adults table and a users table. The Household table contains two foreign keys, iAdult1ID and iAdult2ID. The Users table has a iUserID primary key and the Adult table has a corresponding iUserID foreign key. One of the columns in the Users table is strUsername, an e-mail address.

I am trying to write a query that will allow me to search for an e-mail address for either adult that has a relation to the household. So I have two questions, assuming that all the values are not null, how can I do this?

And two, in reality, iAdult2ID can be null, is it still possible to write a query to do this?

Thanks for your help. Let me know if you need any more information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伊面 2024-10-16 15:41:25

是的,您刚刚将连接保留到同一个表两次:

select u1.strUsername, u2.strUsername
FROM Household h
LEFT JOIN Adult a1 on a1.ID = h.iAdult1ID
LEFT JOIN Users u1 on u1.ID = a1.iUserID
LEFT JOIN Adult a2 on a2.ID = h.iAdult2ID
LEFT JOIN Users u2 ON u2.ID = a2.iUserID

Yes, you just left join to the same table twice:

select u1.strUsername, u2.strUsername
FROM Household h
LEFT JOIN Adult a1 on a1.ID = h.iAdult1ID
LEFT JOIN Users u1 on u1.ID = a1.iUserID
LEFT JOIN Adult a2 on a2.ID = h.iAdult2ID
LEFT JOIN Users u2 ON u2.ID = a2.iUserID
沧笙踏歌 2024-10-16 15:41:25

虽然接受的答案是正确的,但我认为指出原始问题是数据建模方式的结果会有所帮助。

理想情况下,您应该将 AdultIds 从 HouseHold 表移至名为 HouseholdAdults 的新表中,其中包含 HouseholdId 和 iAdultId。然后,每个家庭可以有一名或多名成年人。

这是一个屏幕截图来说明我的意思。


alt text


目前,您已将成人与家庭之间的关系限制为至少一名且不超过两名。没有成人或三个人的房子怎么样?另外,在添加成人记录之前不能先添加家庭记录吗?您在哪里存储有关成人与家庭关系的信息?例如,谁是业主、租户、他们何时入住、何时购买等。基本上与关系相关的任何内容。

目前这些对你来说可能都不是问题,但我认为仍然值得思考。

这种重构可以解决查询数据时遇到的问题。通过新表加入家庭表和成人表,您可以轻松查询以下关系。

SELECT Adult.iAdultId, Household.HouseHoldId, Users.strUserName FROM Adult 
INNER JOIN HouseholdAdults ON Adult.iAdultId = HouseholdAdults.iAdultId 
INNER JOIN Household ON HouseholdAdults.HouseholdId = Household.HouseHoldId 
INNER JOIN Users ON Adult.iUserId = Users.iUserId

这是一些进一步解释的链接。
多对多关系
数据库规范化

While the accepted answer is correct I thought it would be helpful to point out that the original problem is a consequence of how the data is modeled.

Ideally you should move the AdultIds out of the HouseHold table and into a new table called HouseholdAdults which contains both the HouseholdId and the iAdultId. Then you can have one or more adults per household.

Heres a screenshot to illustrate what I mean.


alt text


Currently you have restricted the relationship between adults and household to at least one and no more than two. What about houses with no adults or three? Also, you can't add a household record before adding an adult record? Where do you store information about an adults relationship to a household? For example, who is the owner, tenant, when they moved in, bought it etc. Basically anything relevant to the relationship.

These are all probably non-issues for you at the moment but still worth some thought I think.

This remodeling then resolves the issues you have with querying the data. With the new table in place to join the household table and the adult table you can easily query the relationship with the following.

SELECT Adult.iAdultId, Household.HouseHoldId, Users.strUserName FROM Adult 
INNER JOIN HouseholdAdults ON Adult.iAdultId = HouseholdAdults.iAdultId 
INNER JOIN Household ON HouseholdAdults.HouseholdId = Household.HouseHoldId 
INNER JOIN Users ON Adult.iUserId = Users.iUserId

Heres some links explaining it further.
Many-to-Many relations
Database normalization

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文