can where clause contain OR?

Discussion in 'MySQL' started by promotingspace.net, Sep 1, 2008.

  1. #1
    Hi
    can i have such a sql code?:
    select from table where ( keyword like '%$keyword%' or color like '%$color%' ) and (category=2 or category=3)
    plus you help with this question, do you have a better idea?
     
    promotingspace.net, Sep 1, 2008 IP
  2. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    this is my code:
    <?php
    if($subcat_array==0) $rest=1;
    else{
    $r=1;
    $toadd="";
    while($go_up-$r+1){
    if($subcat_array[$r]){
    //add or if $toadd not empty:
    if($toadd) $toadd.=" OR ";
    $toadd="(ps.subcategoryID='$subcat_array[$r]' AND ps.productID=pp.productID ) ";
    $rest=$rest.$toadd;
    }
    $r++;
    }
    }
        $search="SELECT * FROM `pp.products`,ps.prod_subcat WHERE (`pp.productID` LIKE '%$key%' OR  `pp.productName` LIKE '%$key%' OR `pp.productDescription` LIKE '%$key%' OR `pp.productColour` LIKE '%$key%' OR `pp.productID` LIKE '%$code%' OR `pp.productName` LIKE '%$name%' OR `pp.productColour` LIKE '%$colour%' ORDER BY pp.productID LIMIT $limit1,50 ) AND ($rest)";
    ?>
    
    PHP:
    my purpose is to search a product from the subcategories that their checkbox have been checked
    1 product can be in more than 1 subcategory so the subcat_id is not in product table but the product id and subcategories id are in prod_subcat table.
    So what corrections to the code above is required for it to work?
     
    promotingspace.net, Sep 1, 2008 IP
  3. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    let me divide it :
    1. how can I check all array elements are null? ( line 8)
    2. can I select everything when joing 2 tables? like this:
    
    $search="select * from products, prod_subcat where products.productID=prod_subcat.productID ";
    
    PHP:
    3. i have still problem with sql syntax:
    
        $search="SELECT * FROM `products` WHERE (`productID` LIKE '%$key%' OR  `productName` LIKE '%$key%' OR 
    `productDescription` LIKE '%$key%' OR `productColour` LIKE '%$key%' OR `productID` LIKE '%$code%' OR `productName` LIKE 
    '%$name%' OR `productColour` LIKE '%$colour%' ORDER BY productID LIMIT $limit1,50 ) AND (1)";
    
    PHP:
    will throw out:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY productID LIMIT 0,50 ) AND (1)' at line 1
    thanks
     
    promotingspace.net, Sep 2, 2008 IP
  4. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes.
    You put the ORDER BY and LIMIT statements inside one of the WHERE clauses. Put them at the end.
    Btw, why do you have 'AND (1)' in your query?
     
    CreativeClans, Sep 2, 2008 IP
  5. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks for your help
    and(1) is just for test
    also i can't successfully check this statement.why?:
     if($subcat_array==0)
    PHP:
    $subcat_array is an array
     
    promotingspace.net, Sep 2, 2008 IP