2012-11-20 75 views
0

此時此查詢將搜索名爲article的表,其中的標題和摘要字段包含特定關鍵字。它返回的集合是標題和摘要包含關鍵字的文章,但是我想對其進行更改,以便返回標題或文章的摘要包含關鍵字。我怎麼做到這一點?通過簡單地將內部連接更改爲外部連接?我該如何從執行AND操作切換到OR?

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 

回答

2

以下版本改變了inner join s到left outer join S和增加了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; 
+0

「left outer join JOIN」是一個錯字嗎? – xxyyxx

+0

看起來像是一個錯字。 –

0

看起來你可以在每個INNER JOIN更改爲LEFT JOIN,並且會得到你想要的東西。雖然我承認我不熟悉CONTAINSTABLE

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