SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question SQL WHERE LIKE, multipe statements

    I'm in need of guidance from the masters of SQL the ancient japaneese art of body manipulation .

    I'm trying to create a search engine, currently it all works nicely, kinda. The problem is it only searches one field in my database when i want it to search in two.

    In the database there is a table that is called tblArticles, two of the fields are name, and description. currently it searches the description field but not the name decription. How do I make it search the name field as well?

    current SQL:

    $sql = "SELECT * FROM tblArticles WHERE description LIKE '%" . $HTTP_GET_VARS["strg"] . "%'";

    where $HTTP_GET_VARS["strg"] is the string that I'm searching for.

    Also, think i'm going out of my depth here. Of the string is made up of several words is it possible to split the words up and therefore search for them in any order???
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,336
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    to search in multiple columns, you need multiple WHERE conditions

    i guess the big question is whether you want ANDs or ORs
    Code:
    select something
      from tblArticles
     where name like '%foo%'
       and description like '%bar%'
    if you want several words as parameters, you could string them all together with wildcards
    Code:
      where description like '%foo%bar%qux%'
    but then of course you'd get a match only if all three words were included in that order

    to search for them in any order, you'd want
    Code:
      where description like '%foo%bar%qux%'
        and description like '%foo%bar%qux%'
        and description like '%foo%bar%qux%'
    but here again, because of the ANDs, you'd get a match only if all three words were included

    helps?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, was great help
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,336
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    doh! sorry

    in my haste
    to cut and paste
    i screwed up

    to search for them in any order, you'd want
    Code:
    where description like '%foo%bar%qux%'
      and description like '%foo%bar%qux%'
      and description like '%foo%bar%qux%'
    should be
    Code:
    where description like '%foo%'
      and description like '%bar%'
      and description like '%qux%'
    hopefully you figured that out

  5. #5
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    grins
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk


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
  •