Left Outer Join with Where clause

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DavidPr
    New Member
    • Mar 2007
    • 155

    Left Outer Join with Where clause

    I can't seem to get this working:

    Code:
    $query = "SELECT c.c_id,c.c_name, COUNT( p.recipe_title ) as theCount
    FROM new_recipe_cat AS c
    LEFT OUTER JOIN new_recipes AS p ON c.c_id = p.c_id
    WHERE (c_id==11 && c_id==12 && c_id==13 && c_id==14
    && c_id==15 && c_id==16 && c_id==17 && c_id==22
    && c_id==23)
    AND r_allow=1
    GROUP BY c.c_name";
    The where clause isn't working and it's throwing an error: Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource

    Any ideas on how I can fix this?

    Thanks
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Get the error string from mysql by using mysql_error().

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Yep, you should always check mysql_error() when there is an error in a MySQL query.

      However, I would guess that your problem is with the "c_id==11" checks. There is only supposed to be a single "=" in there. SQL does not use two "=" chars like most programming languages do.

      Comment

      • DavidPr
        New Member
        • Mar 2007
        • 155

        #4
        However, I would guess that your problem is with the "c_id==11" checks. There is only supposed to be a single "=" in there. SQL does not use two "=" chars like most programming languages do.
        Right, I was working with an if clause earlier and the two == was stuck in my mind.

        I tried a few various ways of working the WHERE clause but it just wasn't doing it for me. I used the mysql_error, but it wasn't throwing an error. It just didn't return anything or something that I didn't want. Oh well, I just went back to using an if clause. It list what I want just not in alphabetical order.

        I appreciate all the help. Thanks.

        DavidPr

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          No problem.

          One thing that I don't get though. You used && (and) between all those checks. Are you sure you didn't mean to use || (or)? - Using && your c_id would have had to match all of the numbers per row to return it (which doesn't make sense. Presumably it only has one value).

          You could try the IN() function. - Something like:
          [code=sql]WHERE c_id IN(11, 12, 13, 14, 15, 16, 17, 22, 23)[/code]

          Comment

          Working...