2014-06-13 116 views
0

我不得不在其他人的代碼中解決一個搜索函數返回太多結果的問題,我注意到它使用了MATCH <,其中LIKE更合適。SQL搜索功能問題,LIKE和MATCH

我不是SQL太好,我已經改變了所有的比賽對LIKE的..

這顯然帶來了一個錯誤,我認爲它與針對其即時通訊不知道,但可能是僅做與MATCH一起工作..

這是asp代碼中的聲明。

strSQL = "" 
     strSQL = "SELECT 'case-studies' AS pagetype, cs.title AS entrytitle, cs.casestudyid AS id, csi.casestudyimageid AS imageid, csi.imagetitle, LIKE (cs.title, cs.content) AGAINST ('" & searchfor & "') AS relevance, LIKE (csi.imagetitle) AGAINST ('" & searchfor & "') AS imagerelevance " 
     strSQL = strSQL & "FROM casestudies AS cs, casestudiesimages AS csi " 
     strSQL = strSQL & "WHERE cs.casestudyid = csi.casestudyid AND cs.modestatus = '1' AND ((LIKE(cs.title, cs.content) AGAINST ('" & searchfor & "' IN BOOLEAN MODE)) OR (LIKE(csi.imagetitle) AGAINST ('" & searchfor & "' IN BOOLEAN MODE)) OR (cs.title like '%" & searchfor & "%') OR (cs.content like '%" & searchfor & "%') OR (csi.imagetitle like '%" & searchfor & "%')) " 
     strSQL = strSQL & "UNION ALL " 
     strSQL = strSQL & "SELECT 'news' AS pagetype, news.title AS entrytitle, news.newsid AS id, newsimages.newsimageid AS imageid, newsimages.imagetitle, LIKE (news.title, news.content) AGAINST ('" & searchfor & "') AS relevance, LIKE (newsimages.imagetitle) AGAINST ('" & searchfor & "') AS imagerelevance " 
     strSQL = strSQL & "FROM news, newsimages " 
     strSQL = strSQL & "WHERE news.newsid = newsimages.newsid AND news.modestatus = '1' AND ((LIKE(news.title, news.content) AGAINST ('" & searchfor & "' IN BOOLEAN MODE)) OR (LIKE(newsimages.imagetitle) AGAINST ('" & searchfor & "' IN BOOLEAN MODE)) OR (news.title like '%" & searchfor & "%') OR (news.content like '%" & searchfor & "%') OR (newsimages.imagetitle like '%" & searchfor & "%')) "   
     strSQL = strSQL & "GROUP BY id " 
     strSQL = strSQL & "ORDER BY relevance DESC, imagerelevance DESC " 

誰能幫助破譯這個代碼。它需要搜索像像,而不是搜索「他」和獲得「幫助」等

感謝

回答

0

如果您正在使用MyISAM和有對您要搜索的列全文索引,您可以AVO id使用「MATCH .... AGAINST」而不是LIKE進行全表掃描,效率更高。我在MATCH合適的地方替換了「LIKE」 - 然後在代碼中反對。 SQL的一般規則是,如果要測試的表達式不具有通配符(在這種情況下僅爲%),請使用MATCH。

strSQL = "SELECT 'case-studies' AS pagetype, cs.title AS entrytitle, 
      cs.casestudyid AS id, csi.casestudyimageid AS imageid, csi.imagetitle, 
      MATCH (cs.title, cs.content) AGAINST ('" & searchfor & "') AS relevance, 
      MATCH (csi.imagetitle) AGAINST ('" & searchfor & "') AS imagerelevance " 
strSQL = strSQL & "FROM casestudies AS cs, casestudiesimages AS csi " 
strSQL = strSQL & "WHERE cs.casestudyid = csi.casestudyid AND cs.modestatus = '1' AND 
    ((MATCH (cs.title, cs.content) AGAINST ('" & searchfor & "' IN BOOLEAN MODE)) OR 
    (MATCH (csi.imagetitle) AGAINST ('" & searchfor & "' IN BOOLEAN MODE)) OR 
    (cs.title LIKE '%" & searchfor & "%') OR (cs.content LIKE '%" & 
    searchfor & "%') OR (csi.imagetitle LIKE '%" & searchfor & "%')) " 
strSQL = strSQL & "UNION ALL " 
    strSQL = strSQL & "SELECT 'news' AS pagetype, news.title AS entrytitle, 
     news.newsid AS id, newsimages.newsimageid AS imageid, newsimages.imagetitle, 
     MATCH (news.title, news.content) 
     AGINST ('" & searchfor & "') AS relevance, 
     MATCH (newsimages.imagetitle) 
     AGAINST ('" & searchfor & "') AS imagerelevance " 
    strSQL = strSQL & "FROM news, newsimages " 
    strSQL = strSQL & "WHERE news.newsid = newsimages.newsid AND news.modestatus = '1' 
     AND ((MATCH (news.title, news.content) 
       AGAINST ('" & searchfor & "' IN BOOLEAN MODE)) OR 
     (MATCH (newsimages.imagetitle) AGAINST ('" & searchfor & "' 
     IN BOOLEAN MODE)) OR (news.title LIKE '%" & searchfor & "%') 
     OR (news.content LIKE '%" & searchfor & "%') 
     OR (newsimages.imagetitle LIKE '%" & searchfor & "%')) "   
    strSQL = strSQL & "GROUP BY id " 
    strSQL = strSQL & "ORDER BY relevance DESC, imagerelevance DESC " 
1

你是說喜歡,但你是不是你比較喜歡的任何東西,一個有效的LIKE語句是

SELECT * 
FROM CUSTOMERS 
WHERE CUSTOMERNAME LIKE '%' + @Param+ '%' 
+0

好的,我明白你的意思了,我所做的只是將MATCH更改爲LIKE,所以我認爲這一切都是錯誤的......問題在於我缺乏SQL知識。你可以連續使用多個LIKE嗎? –

+0

嗨保羅,是的,你可以在你的發言中有多個LIKE。但是你將永遠需要像上面那樣定義它。所以然後你添加AND然後像...一樣的列名,同樣的方式再次:) –