sql Search

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • grabit
    New Member
    • Mar 2007
    • 22

    sql Search

    Hi peoples
    I have the following query to return search results. How can i stop this from returning results containg words or phrases like
    "Hi Team, please check latest news. Cheers Rach"
    when i search for "test" and the search string is contained in the word "latest"

    I just want to search for and return the whole words

    Query is
    <cfquery name="searchRes ults" datasource="#ds n#">
    SELECT threadID, posttype, topic, topicID, postdate, username, threads.catID, posttext, categories.catI D, category
    FROM threads INNER JOIN categories
    ON threads.catID=c ategories.catID
    WHERE topic LIKE '%#form.subject #%' OR username LIKE '%#form.subject #%' OR posttext LIKE '%#form.subject #%'
    </cfquery>
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    You need to impose spaces between the search word and the wildcards as follows:

    Code:
    <cfquery name="searchResults" datasource="#dsn#">
    SELECT threadID, posttype, topic, topicID, postdate, username, threads.catID, posttext, categories.catID, category
    FROM threads INNER JOIN categories
    ON threads.catID=categories.catID 
    WHERE topic LIKE '% ' & '#form.subject#' & ' %' 
    OR username LIKE  '% ' & '#form.subject#' & ' %' 
    OR posttext LIKE '% ' & '#form.subject#' & ' %' 
    </cfquery>

    Comment

    • MMcCarthy
      Recognized Expert MVP
      • Aug 2006
      • 14387

      #3
      If you need to include words at the beginning and end of sentences which don't have spaces before and after them then you will need to allow for that in the code as follows:
      Code:
      WHERE ((topic LIKE '% #form.subject# %' 
      OR topic LIKE '#form.subject# %'
      OR topic LIKE '% #form.subject#')
      OR (username LIKE  '% #form.subject# %' 
      OR username LIKE  '#form.subject# %' 
      OR username LIKE  '% #form.subject#')
      OR (posttext LIKE '% #form.subject# %' 
      OR posttext LIKE '#form.subject# %'
      OR posttext LIKE '% #form.subject#'))
      Watch the brackets.

      Mary

      Comment

      • grabit
        New Member
        • Mar 2007
        • 22

        #4
        Originally posted by mmccarthy
        If you need to include words at the beginning and end of sentences which don't have spaces before and after them then you will need to allow for that in the code as follows:
        Code:
        WHERE ((topic LIKE '% #form.subject# %' 
        OR topic LIKE '#form.subject# %'
        OR topic LIKE '% #form.subject#')
        OR (username LIKE  '% #form.subject# %' 
        OR username LIKE  '#form.subject# %' 
        OR username LIKE  '% #form.subject#')
        OR (posttext LIKE '% #form.subject# %' 
        OR posttext LIKE '#form.subject# %'
        OR posttext LIKE '% #form.subject#'))
        Watch the brackets.

        Mary
        I have this which seems to work
        <cfquery name="searchRes ults" datasource="#ds n#">
        SELECT threadID, posttype, topic, topicID, postdate, username, threads.catID, posttext, categories.catI D, category
        FROM threads INNER JOIN categories
        ON threads.catID=c ategories.catID
        WHERE topic LIKE '#form.subject# %' OR topic LIKE '% #form.subject#' OR topic LIKE '% #form.subject# %' OR topic LIKE '#form.subject# '
        OR username LIKE '#form.subject# '
        OR posttext LIKE '#form.subject# %' OR posttext LIKE '% #form.subject#' OR posttext LIKE '% #form.subject# %' OR posttext LIKE '#form.subject# '
        </cfquery>

        Comment

        • MMcCarthy
          Recognized Expert MVP
          • Aug 2006
          • 14387

          #5
          Sounds good, the bracket just make it tidier but shouldn't affect preformance.

          Mary

          Comment

          Working...