我想从我的SQL服务器数据库中选择数据,其中6个表通过外键连接在一起。为了根据表4中的参数从某些列中选择数据,而其他列位于其他表中。表格中的关系和我的代码如下。 表1与表2,表3,表4,表5有关 表2与表3,表4有关。此代码不适用于产品或其他表。它只是用于人,而我想根据产品的名称进行选择。
select
Person.Name,
Person.Last_Name,
Call.Email,
Call.Email_t,
Call.Mobile,
Call.mobile_two,
Call.Website,
Person.expert_one,
Person.expert_one,
Organization.Name,
Product.Product_Name,
Patent.Pat_Name
from Person
inner join Call on Person.Call_Id = Call.ID
right join Organization on Person.Organize_ID = Organization.ID
left join Product on Product.Person_Id_o = Person.ID and Product.Person_Id_t = Person.ID
left join Patent on Patent.Person_Id_o = Person.ID and Patent.person_two = Person.ID and Patent.person_three = Person.ID and patent.Person_four = Person.ID
right join Education on Education.Person_ID = Person.ID
where Product.Product_Name LIKE N'%name%'
答案 0 :(得分:0)
因此,您可以更改连接并使用Product表作为基表。
例如:
...
from Product
left join Person on (Person.ID = Product.Person_Id_o and Person.ID = Product.Person_Id_t)
left join Call on Call.ID = Person.Call_Id
left join Organization Org on Org.ID = Person.Organize_ID
left join Patent on (Patent.Person_Id_o = Person.ID and Patent.person_two = Person.ID and Patent.person_three = Person.ID and patent.Person_four = Person.ID)
left join Education on Education.Person_ID = Person.ID
where Product.Product_Name LIKE N'%name%'
根据您想要限制的方式,其中一些左连接可以更改为内连接。
但说实话,我发现加入专利有点奇怪 所有这4个人的身份真的需要是同一个身份证吗?