[英]Multiple foreign keys from one table linking to single primary key in second table
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. 家用表包含两个外键,iAdult1ID和iAdult2ID。 The Users table has a iUserID primary key and the Adult table has a corresponding iUserID foreign key. Users表具有一个iUserID主键,而Adult表具有一个对应的iUserID外键。 One of the columns in the Users table is strUsername, an e-mail address. “用户”表中的列之一是strUsername(电子邮件地址)。
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? 所以我有两个问题,假设所有值都不为null,我该怎么做?
And two, in reality, iAdult2ID can be null, is it still possible to write a query to do this? 还有两个,实际上,iAdult2ID可以为null,是否仍然可以编写查询来执行此操作?
Thanks for your help. 谢谢你的帮助。 Let me know if you need any more information. 让我知道您是否需要更多信息。
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
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. 理想情况下,您应该将AdultIds从HouseHold表中移出,并移到一个名为HouseholdAdults的新表中,该表同时包含HouseholdId和iAdultId。 Then you can have one or more adults per household. 这样一来,每个家庭可以有一个或多个成年人。
Heres a screenshot to illustrate what I mean. 这是截图,以说明我的意思。
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 数据库规范化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.