SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Need correct syntax for WHERE clause using multiple OR

    I need the correct syntax for the following. The following works (it correctly searches and retrieves the term for searching), but I know there is a shorter way to write the WHERE clause to search the three columns:

    PHP Code:
    $searchtext $_POST['searchtext'];
    if (
    $searchtext != '') { // Some search text was specified
      
    $where .= " AND vehicle_name LIKE '%$searchtext%' OR owner_name LIKE '%$searchtext%' OR descr LIKE '%$searchtext%'";

    This short form does not work:

    PHP Code:
    AND vehicle_name, OR owner_name, OR descr LIKE '%$searchtext%'"; 
    This doesn't work either:

    PHP Code:
    AND vehicle_nameowner_namedescr LIKE '%$searchtext%'"; 
    The basic code comes from page 127 of Kevin's Build Your Own DB Driven Website book.

    Thanks!

    Steve

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Code:
    AND (
        vehicle_name LIKE '%$searchtext%'
     OR owner_name LIKE '%$searchtext%'
     OR descr LIKE '%$searchtext%'
        )
    the parentheses are extremely important since you have an AND which combines these conditions with the rest of the WHERE clause
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OK, thanks. So there is no shorter way to do it. I appreciate the reply!

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    well, there is one way, but i don't like it...
    Code:
    AND CONCAT(vehicle_name, owner_name, descr)
           LIKE '%$searchtext%'
    occasionally you might get a false hit if the string you're looking for happens to span two columns
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •