sql搜索功能问题,LIKE和MATCH

时间:2014-06-13 11:16:25

标签: sql match sql-like

我不得不在其他人的代码中解决问题,其中搜索功能返回的结果太多,我注意到它正在使用MATCH< LIKE更合适。

我对SQL不太了解,我已将所有MATCH改为LIKE&#39>

这显然会带来错误,我认为它与AGAINST有关,我不确定但只能与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 "

任何人都可以帮助解密这段代码..它需要搜索喜欢的东西,而不是搜索"他"获得"帮助"等

由于

2 个答案:

答案 0 :(得分:1)

你说的是LIKE但是你没有将LIKE与任何东西进行比较,有效的LIKE语句是

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

答案 1 :(得分:0)

如果您正在使用MyISAM并且要搜索的列上有全文索引,则可以使用“MATCH .... AGAINST”而不是LIKE来避免全表扫描,这样效率会更高。我已经在MATCH合适的地方替换了“LIKE” - 然后在你的代码中使用了AGAINST。 SQL的一般规则是,如果要测试的表达式没有通配符(在这种情况下仅为ON),请使用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 "