2017-02-11 27 views
1

我在數據庫中有兩個文件名(Text和Text1) Text.txt內容包含「data」。 Text1.txt內容包括「data」和「data1」。檢索最相似的內容並顯示爲SQL中的第一個結果

enter image description here

SQL查詢,我寫道:

SELECT [FileName], [FilePath] FROM dbo.[tb_CrawlData] cr 
    WHERE EXISTS (SELECT 1 FROM [tb_CrawlData] cd 
       WHERE cd.Content like '%data%' AND 
        cr.Content like '%' + cd.Content + '%' 
      ) 
    GROUP BY cr.FileName, [FilePath] 
    ORDER BY cr.FileName 

結果:

FileName FilePath 
Text.txt H:\Text.txt 
Text1.txt H:\New folder (2)\Text1.txt 

預期結果:

如果我搜索 「數據」,我想獲得Text1.txt首先在我的第一行 ,因爲它有兩個相似的內容(「數據」和「數據1」)。 Text.txt應顯示第二行,因爲它僅包含(「數據」)。

回答

0

order by子句中只需添加一個count(*) desc

select [FileName], 
    [FilePath] 
from dbo.[tb_CrawlData] cr 
where exists (
     select 1 
     from [tb_CrawlData] cd 
     where cd.Content like '%data%' 
      and cr.Content like '%' + cd.Content + '%' 
     ) 
group by cr.FileName, 
    [FilePath] 
order by count(*) desc, 
    cr.FileName 
+0

@GurV ......太感謝你了 這正是我想要的。 –

相關問題