我正在管理一个用于组织学者及其专业的数据库。学术人员可以拥有多个专业,而专业可以分配给不止一个人。相关表包含有关上述多对多关系建模的学者(数据库),关键字表(Keywords)和连接表(KeywordJunctionMk2)的信息。我知道这种命名约定不好,但是现在不是重点。
我正在尝试创建一个高级搜索,允许用户从上表中搜索各种条件,包括但不限于姓名,地址,联系电话和关键字。重要的是,我希望用户可以通过以下方式搜索关键字:“显示拥有记录的人的记录” Keyword1 AND(关键字2或关键字3)。
问题是,当我在每个选项中输入不同的关键字时,结果表中的名称就会重复。例如。如果我搜索“航空”,“与”(“运输”或“工程”),如果他们拥有所有这些关键字,则两次获得相同的人。我希望这样,即使一个人在“关键字连接”表中有多个相关记录,也只会在搜索中出现一次。
这是当前查询:
SELECT Database.[Record Type], Database.[Full Name], Database.[Date created],
Database.[Job Title], Database.Notes, Database.[Town/City], Database.[Created
by], Database.Email, Database.Contact_ID, Database.InstitutionLookup,
KeywordJunctionMk2.Keyword_ID, KeywordJunctionMk2_1.Keyword_ID, Database.
[Work phone], Database.Mobile, Database.[Work phone 2]
FROM Keywords
INNER JOIN (Institution INNER JOIN (([Database] INNER JOIN
KeywordJunctionMk2 AS KeywordJunctionMk2_1 ON Database.Contact_ID =
KeywordJunctionMk2_1.Contact_ID) INNER JOIN KeywordJunctionMk2 ON
Database.Contact_ID = KeywordJunctionMk2.Contact_ID) ON Institution.ID =
Database.InstitutionLookup) ON Keywords.Keyword_ID =
KeywordJunctionMk2.Keyword_ID
WHERE (((Database.[Record Type])=IIf([Forms]![New Advanced Search]!
[Contact_Btn]=-1,"Contact",Null)
Or (Database.[Record Type])=IIf([Forms]![New Advanced Search]!
[Expert_Btn]=-1,"Expert",Null)
Or (Database.[Record Type])=IIf([Forms]![New Advanced Search]!
[Journalist_Btn]=-1,"Journalist",Null)
Or (Database.[Record Type])=IIf([Forms]![New Advanced Search]!
[PressOfficer_Btn]=-1,"Press Officer",Null)
Or (Database.[Record Type])=IIf([Forms]![New Advanced Search]!
[IndustryScientist_Btn]=-1,"Industry Scientist",Null)
Or (Database.[Record Type])=IIf([Forms]![New Advanced Search]!
[IndustryPressOfficer_Btn]=-1,"Industry Press Officer",Null)
Or (Database.[Record Type])=IIf([Forms]![New Advanced Search]!
[Scientist_Btn]=-1,"Scientist",Null))
AND ((Database.[Full Name]) Like "*" & [Forms]![New Advanced Search]![Name] &
"*" Or (Database.[Full Name]) Is Null)
AND ((Database.[Date created])>[Forms]![New Advanced Search]![DateCreated] Or
(Database.[Date created]) Is Null)
AND ((Database.[Job Title]) Like "*" & [Forms]![New Advanced Search]!
[JobTitle] & "*" Or (Database.[Job Title]) Is Null)
AND ((Database.Notes) Like "*" & [Forms]![New Advanced Search]![Notes] &
"*"Or (Database.Notes) Is Null)
AND ((Database.[Town/City]) Like "*" & [Forms]![New Advanced Search]!
[Location] & "*" Or (Database.[Town/City]) Is Null)
AND ((Database.[Created by]) Like "*" & [Forms]![New Advanced Search]!
[CreatedBy] & "*" Or (Database.[Created by]) Is Null)
AND ((Database.Email) Like "*" & [Forms]![New Advanced Search]![Email] & "*"
Or (Database.Email) Is Null)
AND ((Database.InstitutionLookup) Like "*" & [Forms]![New Advanced Search]!
[Institution] & "*" Or (Database.InstitutionLookup) Is Null)
AND ((KeywordJunctionMk2.Keyword_ID)=[Forms]![New Advanced Search]!
[Keywords1] Or (KeywordJunctionMk2.Keyword_ID) Is Null)
AND ((KeywordJunctionMk2_1.Keyword_ID)=[Forms]![New Advanced Search]!
[Keywords2] Or (KeywordJunctionMk2_1.Keyword_ID) Is Null Or
((KeywordJunctionMk2_1.Keyword_ID)=[Forms]![New Advanced Search]![Keywords3]
Or (KeywordJunctionMk2_1.Keyword_ID) Is Null))
AND ((Database.[Work phone]) Like "*" & [Forms]![New Advanced Search]!
[WorkNumber] & "*" Or (Database.[Work phone]) Is Null)
AND ((Database.Mobile) Like "*" & [Forms]![New Advanced Search]![Mobile] &
"*" Or (Database.Mobile) Is Null)
AND ((Database.[Work phone 2]) Like "*" & [Forms]![New Advanced Search]!
[WorkNumber] & "*" Or (Database.[Work phone 2]) Is Null))
ORDER BY Database.[Record Type] DESC;
我已经分离出处理关键字的WHERE子句的相关部分,因为这是我遇到的问题。
我已经通过仅使用“关键字”字段创建查询来测试了此问题,并且该问题已在此处复制。我也尝试过使用UNION查询和SELECT DISTINCT,但是我仍然得到重复的名称。我正在探索的另一个解决方案是左/右联接的组合,但是我还没有运气。
赞赏有关可能解决方案的任何建议。