2010-06-07 59 views
0

我遇到问题,我创建了一个全文搜索查询,它返回一个记录,其中每个记录都提供了匹配项字段(全文索引)多个表。问题是,当user.id等于ceritification.AId时,它返回记录事件,但它不满足提供的参数。在多个表中使用SQL Server 2005中的全文搜索,列

对于这个例子,我提供了一个返回Id = 184的值“xandrick”,但问题是它返回了两个id,分别是184和154。返回满足以下条件的ID的最好方法是什么?提供的给定值?

User table 
Id Firstname  Lastname Middlename  Email      AlternativeEmail 
154 Gregorio Honasan Pimentel  [email protected]  [email protected] 
156 Qwerty Qazggf fgfgf  [email protected]  [email protected] 
184 Xandrick Flores NULL  [email protected]    null 

认证表

Id AID Certification     School 
12 184 sdssd       AMA 
13 43 web-based and framework 2  Asian development foundation college 
16 184 hjhjhj      STI 
17 184 rrrer       PUP 
18 154 vbvbv       AMA 

SELECT DISTINCT Users.Id 
FROM Users 
INNER JOIN Certification on Users.Id=Certification.aid 
LEFT JOIN 
FREETEXTTABLE (Users,(Firstname,Middlename,Lastname,Email,AlternativeEmail), 'xandrick')as ftUsr ON Users.Id=ftUsr.[KEY] 
LEFT JOIN 
FREETEXTTABLE (Certification,(Certification,School), 'xandrick')as ftCert ON Certification.Id=ftCert.[KEY] 
+1

当您发布代码和/或表格结构时,**请**突出显示这些行并按下编辑器工具栏上的“代码”按钮(101 010)。这将很好地格式化和语法高亮这些代码片段,让你的文章更容易阅读和理解!并尝试**不**以使用TABS - 将它们转换为空格! – 2010-06-07 09:43:56

回答

1
SELECT u.Id 
FROM Users u 
WHERE FREETEXT(*,'"BPI"') 
UNION 
SELECT c.AId 
FROM Certification c 
WHERE FREETEXT(*,'"BPI"') 
UNION 
SELECT ad.AId 
FROM ApplicantDetails ad 
WHERE FREETEXT(*,'"BPI"') 
UNION 
SELECT eb.AId 
FROM EducationalBackground eb 
WHERE FREETEXT(*,'"BPI"') 
UNION 
SELECT ed.AId 
FROM EmploymentDetails ed 
WHERE FREETEXT(*,'"BPI"') 
UNION 
SELECT e.AId 
FROM Expertise e 
WHERE FREETEXT(*,'"BPI"') 
UNION 
SELECT ge.AId 
FROM GeographicalExperience ge 
WHERE FREETEXT(*,'"BPI"') 
UNION 
SELECT pd.AId 
FROM ProjectDetails pd 
WHERE FREETEXT(*,'"BPI"') 
UNION 
SELECT r.AId 
FROM [References] r 
WHERE FREETEXT(*,'"BPI"') 
UNION 
SELECT t.AId 
FROM Training t 
WHERE FREETEXT(*,'"BPI"') 
0
Another solution but this is very slow compare the below query. 

select DISTINCT u.Id from Users u 
inner join Certification c on u.ID = 
c.AId inner join ApplicantDetails ad 
on u.ID=ad.AId inner join 
EducationalBackground eb on 
u.ID=eb.AId inner join 
EmploymentDetails ed on u.Id=ed.AId 
inner join Expertise e on u.Id=e.AId 
inner join GeographicalExperience ge 
on u.Id=ge.AId inner join [Language] l 
on u.Id=l.AId inner join 
ProjectDetails pd on u.Id=pd.AId 
inner join [References] r on 
u.Id=r.AId inner join Training t on 
u.Id=t.AId left join FreeTexttable 
(Users, 
(AlternativeEmail,Email,Firstname,Lastname,Middlename), 
'xandrick') as uf on uf.[KEY] = 
u.id left join FreeTexttable 
(ApplicantDetails, 
(Address1,Address2,City,Province,StateorRegion), 
'xandrick') as adf on adf.[KEY] = 
ad.id left join FreeTexttable 
(Certification, 
(Certification,School), 'xandrick') 
as cf on cf.[KEY] = c.id left join 
FreeTexttable (EducationalBackground, 
(fieldofStudy,other,School), 
'xandrick') as ebf on ebf.[KEY] = 
eb.id left join FreeTexttable 
(EmploymentDetails, 
(Address1,Address2,City,CompanyName,DescriptionofDuties,Position,Province,TypeofBusiness), 
'xandrick') as edf on edf.[KEY] = 
ed.id left join FreeTexttable 
(Expertise, (Expertise), 'xandrick') 
as ef on ef.[KEY] = e.id left join 
FreeTexttable (GeographicalExperience, 
([Description]), 'xandrick') as gef 
on gef.[KEY] = ge.id left join 
FreeTexttable ([Language], 
([Language]), 'xandrick') as lf on 
lf.[KEY] = l.id left join 
FreeTexttable (ProjectDetails, 
(Address1,Address2,City,ProjectDescription,Projectname,Projectrole,Province,ServiceRendered,StateorRegion), 
'xandrick') as pdf on pdf.[KEY] = 
pd.id left join FreeTexttable 
([References], 
(ContactDetails,CurrentPosition,Name,Organization), 
'xandrick') as rf on rf.[KEY] = 
r.id left join FreeTexttable 
(Training, (School,Training), 
'xandrick') as tf on tf.[KEY] = 
t.id 

where uf.[KEY] is not null OR 
adf.[KEY] is not null OR cf.[KEY] is 
not null OR ebf.[KEY] is not null 
OR edf.[KEY] is not null OR ef.[KEY] 
is not null OR gef.[KEY] is not null 
OR lf.[KEY] is not null OR pdf.[KEY] 
is not null OR rf.[KEY] is not null 
OR tf.[KEY] is not null 
0

到目前为止,这样做的绝对是最好的方法是使用相结合的表有问题的索引视图。将您的自由文本索引添加到视图中,然后将其用于搜索。

不管你信不信,但它比运行多个freetexttable子句要快得多。

+0

嗯,我正在实施使用索引视图,我会在这里发布它,一旦我做完了它 – user335160 2010-06-08 05:51:07

相关问题