我已经以表格形式提供了几张桌子

主记录

Record ID  Details  Textual Information
    1      AAAAAAA  ... some text referring to Oxford St Giles...
    2      BBBBBBB  ... some text referring Oxford....
    3      CCCCCCC  ... some text referring to Oxford St Aldate...


和支撑台

地名

Record ID   PlaceName
    1        Oxford
    1        St
    1        Giles
    2        Oxford
    3        Oxford
    3        St
    3        Aldate


我希望能够构建一个搜索词,以便可以在地名上输入全部或部分词。例如。如果我输入“ Oxford”,我会得到所有3条记录,如果我输入“ Oxford”和“ Giles”,我只会得到记录1-几乎就像使用WHERE IN(“ Oxford”,“ Giles”)一样,但这些术语是ANDed而不是被ORed?

我不知道我能做到吗?我尝试了各种子查询,但均未成功

我正在使用SQL Server 2008

我想避免在全文搜索字段中输入

任何清除薄雾的指针将非常有帮助。

*更新了主要记录的详细信息,以避免造成混淆*
2个表之间的唯一链接是记录ID

**更新** 11月3日带有示例表

CREATE TABLE MAIN_RECORD (RecordID int,DocumentRef varchar(100));
INSERT INTO MAIN_RECORD VALUES (86, 'Doc Referring to William Samuel ADAMS');
INSERT INTO MAIN_RECORD VALUES (87, 'Doc Referring to William JONES');
INSERT INTO MAIN_RECORD VALUES (88, 'Doc Referring to Samuel SMITH');



CREATE TABLE FORENAMES (RecordID int,Forename varchar(25));
INSERT INTO FORENAMES VALUES (86, 'William');
INSERT INTO FORENAMES VALUES (86, 'Samuel');
INSERT INTO FORENAMES VALUES (87, 'William');
INSERT INTO FORENAMES VALUES (88, 'Samuel');


我的初始查询是

SELECT main.[RecordID],main.documentRef
    FROM [MAIN_RECORD] main
    INNER JOIN  [FORENAMES] fn
    ON main.RecordID = fn.RecordID
    WHERE fn.ForeName IN ('William')


很好,然后返回

RecordID    documentRef
86  Doc Referring to William Samuel ADAMS
87  Doc Referring to William JONES


与塞缪尔同上

我的问题是当我在``地名搜索''字段中有多个条目时,即

SELECT main.[RecordID],main.documentRef
    FROM [MAIN_RECORD] main
    INNER JOIN  [FORENAMES] fn
    ON main.RecordID = fn.RecordID
    WHERE fn.ForeName IN ('William,Samuel')


这什么也不会返回。

我只需要返回同时包含塞缪尔和威廉的MAIN记录。当搜索词中包含多个名称时。
它还需要找到William Samuel和Samuel William。

从其他人的帖子中,我得出了DIVISION的思路,并提出了以下建议(在main SELECT之前加上一些字符串操作):

DECLARE @Forename nvarchar(max)
DECLARE @SQLCommand nvarchar(max)
DECLARE @Number_of_Terms int
SET @Forename = 'William,Samuel'
--SET @Forename = 'Samuel,William'
--SET @Forename = 'William'
--SET @Forename = 'Samuel'
SET @Number_of_Terms = LEN(@Forename) - LEN(REPLACE(@Forename,',',''))+1
SET @Forename = REPLACE(@Forename,',',''',''')

SET @SQLCommand = 'SELECT fr.RecordID FROM dbo.BRS109_FullRecord fr '+
'INNER JOIN dbo.BRS109_Forenames fn '+
'ON fr.RecordID = fn.RecordID '+
'WHERE fr.RecordID = fn.RecordID '+
'AND fn.forename IN ('''+@Forename +''') ' +
' GROUP BY fr.RecordID ' +
' HAVING COUNT(fr.RecordId) = ' + CAST(@Number_of_Terms AS varchar(2)) +
' ORDER BY fr.RecordId'

EXECUTE sp_executesql @SQLCommand


这似乎给了我我想要的东西。

非常感谢大家特别贡献了“ Quassnoi”和“ onedaywhen”-非常有帮助

最佳答案

SELECT  *
FROM    mainrecord mr
WHERE   (
        SELECT  COUNT(*)
        FROM    placenames pn
        WHERE   pn.record = mr.record
                AND pn.placename IN ('Oxford', 'St', 'Giles')
        ) = 3

08-03 17:26