0

现在,这个查询在一个名为 article 的表中搜索标题和摘要字段包含某个关键字的条目。它返回的集合是标题和摘要包含关键字的文章,但我想更改它,以便它返回标题或摘要包含关键字的文章。我将如何做到这一点?通过简单地将内部连接更改为外部连接?

BEGIN
    with articlesearch as (
    SELECT top 1000 FT_TBL.articleID, FT_TBL.title,FT_TBL.abstract,FT_TBL.publicationdate,
        (select j.journalID from journal  j where FT_TBL.journalID=j.journalID) as sourceID,
        (select j.journalname from journal  j where FT_TBL.journalID=j.journalID) as sourcename,
        (select j2.medabbr from journal j2 where FT_TBL.journalID=j2.journalID) as medabbr,
        (select j1.impactfactor from journal  j1 where FT_TBL.journalID=j1.journalID) as impactfactor,
        KEY_TBL.RANK,
        ROW_NUMBER() OVER (ORDER BY KEY_TBL.RANK desc) AS RowNumber
    FROM article AS FT_TBL 
        INNER JOIN 
            CONTAINSTABLE(article,title,@keyword) AS KEY_TBL
            ON FT_TBL.articleID = KEY_TBL.[KEY]
        INNER JOIN
            CONTAINSTABLE(article,abstract,@keyword) AS KEY_TBL2
            ON FT_TBL.articleID = KEY_TBL2.[KEY]
    where  FT_TBL.inactive=0
    ORDER BY RANK DESC
    )

    SELECT articleID, sourcename,title,abstract,publicationdate,medabbr  
    FROM articlesearch
    WHERE RowNumber BETWEEN @RowStart AND @RowEnd ORDER BY publicationdate desc;

END 
4

3 回答 3

2

以下版本将 s 更改inner joinleft outer joins 并添加一个where子句以获得您想要的内容:

with articlesearch as (
SELECT top 1000 FT_TBL.articleID, FT_TBL.title,FT_TBL.abstract,FT_TBL.publicationdate,
    (select j.journalID from journal  j where FT_TBL.journalID=j.journalID) as sourceID,
    (select j.journalname from journal  j where FT_TBL.journalID=j.journalID) as sourcename,
    (select j2.medabbr from journal j2 where FT_TBL.journalID=j2.journalID) as medabbr,
    (select j1.impactfactor from journal  j1 where FT_TBL.journalID=j1.journalID) as impactfactor,
    KEY_TBL.RANK,
    ROW_NUMBER() OVER (ORDER BY KEY_TBL.RANK desc) AS RowNumber
FROM article AS FT_TBL 
    left outer JOIN 
        CONTAINSTABLE(article,title,@keyword) AS KEY_TBL
        ON FT_TBL.articleID = KEY_TBL.[KEY]
    left outer join 
        CONTAINSTABLE(article,abstract,@keyword) AS KEY_TBL2
        ON FT_TBL.articleID = KEY_TBL2.[KEY]
where FT_TBL.inactive=0 and (key_tbl.[key] is not null or key_tbl2.[key] is not null)
ORDER BY RANK DESC
)

SELECT articleID, sourcename,title,abstract,publicationdate,medabbr  
FROM articlesearch
WHERE RowNumber BETWEEN @RowStart AND @RowEnd ORDER BY publicationdate desc;
于 2012-11-20T23:04:37.593 回答
0

看起来你可以将INNER JOIN每个更改为 a LEFT JOIN,这会让你得到你想要的。虽然我承认我不熟悉CONTAINSTABLE

于 2012-11-20T22:52:09.810 回答
0

正如 DigitalD 所指出的,您需要将 更改INNER JOINOUTER,但是您还需要过滤至少有一个连接不为空的结果。

-- snip
----
-- /snip
FROM article AS FT_TBL 
    LEFT OUTER JOIN 
        CONTAINSTABLE(article,title,@keyword) AS KEY_TBL
        ON FT_TBL.articleID = KEY_TBL.[KEY]
    LEFT OUTER JOIN
        CONTAINSTABLE(article,abstract,@keyword) AS KEY_TBL2
        ON FT_TBL.articleID = KEY_TBL2.[KEY]
WHERE  FT_TBL.inactive=0
    AND (KEY_TBL.[KEY] IS NOT NULL OR KEY_TBL2.[KEY] IS NOT NULL)
ORDER BY RANK DESC
)

-- snip
----
-- /snip
于 2012-11-20T23:09:47.523 回答