Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Thanked 0 Times in 0 Posts

    Post WHERE adn OR query problem

    Hello, can anybody help me with this query please.

    I have tbl_project that has a row primary `category` which acts as the main decision maker as to what gets selected,
    WHERE category LIKE \'%' . $category . '%\'

    but it is possible that a row may not have the `category` equaling $category, but it may have a secondary link relationship to the category ( $linkedName = 0 or 1 to determine yes or no relationship) and I would like to include this in the query.
    OR \'' . $linkedName . '\' = 1

    And that's the part that I'm having trouble with. I think I need to insert the above OR line in after the WHERE line, but it's not giving me a result I desire.
    Any help on this would be much appreciated. (I am searching other forums etc but I've not found anything that has helped me yet).

    //select projects from database matching chosen category
    $query = 'SELECT
    \'' . $linkedName . '\',
    FROM `tbl_project` `a`, `tbl_projectimages` `b`
    WHERE category LIKE \'%' . $category . '%\'

    OR \'' . $linkedName . '\' = 1 //this line is giving a confused output, the query works well without this line, but I would like the extra linked results

    AND `a`.`projectId` = `b`.`projectId`
    AND `selector` = 0
    ORDER BY `a`.`projectId` DESC';

    Thanks in advance to anybody that looks into my problem.

    this question also in weberforums.com

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Wake Forest, North Carolina
    Thanked 208 Times in 205 Posts
    You need to group your WHERE clauses correctly to get the desire result.

    $query = "SELECT `a`.`projectId`, `projectName`, `category`, `b`.`imgUrl`, `status`, `alt`, '$linkedName ', `b`.`selector` " .
    "FROM `tbl_project` `a`, `tbl_projectimages` `b`" .
    "WHERE (category LIKE '%$category%' OR '$linkedName' = 1) " .
    "AND `a`.`projectId` = `b`.`projectId` AND `selector` = 0 " .
    "ORDER BY `a`.`projectId` DESC';"
    Dave .... HostMonster for all of your hosting needs

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,745 Times in 4,707 Posts
    To explain:

    First answer this question:
        What is the value of 3 + 4 * 2 ?
    If you answered 14, go back to school.

    Multiplication has higher precedence than addition, so you should multiply 4 * 2 first and then add the 3. The answer is 11. If you *wanted* an answer of 14, you would write
         ( 3 + 4 ) * 2
    Same situation with AND and OR. AND has higher precedence.

    So if you write
        WHERE a OR b AND c
    you have *really* coded
        WHERE a OR ( b AND c )
    and, DJM pointed out, if you want it the other way you must *explicitly* use parentheses:
        WHERE ( a OR b ) AND c
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


    Posting Permissions

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