SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    739
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question MySQL Search Query

    Hi,

    You might have seen on websites that they allow you to search by:

    "Exact word"
    "All Words"
    "Any words"

    So for the first one i.e exact word, we use the SQL clause WHERE TITLE = 'xyz'
    For the "all words" search we use the SQL clause WHERE TITLE LIKE '%xyz%'

    (Pls correct me if I am wrong with the above)

    I was wondering how do we make an mysql search for search strings like "hello world", such that if either the word "hello" is found anywhere in the TITLE column or the word "world" is found in the TITLE column, it should display the matching results.

    Pls can anyone help me out?

    Thanks

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Exact word/phrase:

    WHERE title LIKE '%hello world%'

    Any word:

    WHERE title LIKE '%hello%' OR title LIKE '%world%'

    All words:

    WHERE title LIKE '%hello%' AND title LIKE '%world%'

    Note there will be some work your application logic will have to carry out in order to split a search term up into multiple words, if necessary for the search
    Sam Hastings


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •