SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Feb 2008
    Posts
    655
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Bit of confusion with a basic join

    Here is the MySQL query I am using:

    Code MySQL:
    SELECT * FROM wc_poems p, wc_poem_cat_link l WHERE p.id=l.poem_id AND (l.cat_id IN (3) OR p.all_cats=1) ORDER BY p.list_order ASC

    The query should return a column if all_cats is set to 1, no matter whether the other conditions are met or not. But it only returns if cat_id matches the condition (i.e. it is 3).

    Using EXPLAIN I get the message "Impossible WHERE noticed after reading const table..." I though I had some idea of what that meant, but can't put my finger on it.

    If somebody could help I would appreciate it. If there is some more information I need to post please let me know.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,031
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Let me rewrite that for you:

    Code MySQL:
    SELECT
       something
     , anything
     , just_not_the_dreaded_star
    FROM
      wc_poems p
         INNER JOIN
      wc_poem_cat_link l
         ON
      p.id=l.poem_id
    WHERE
       l.cat_id=3
          OR
       p.all_cats=1
    ORDER BY
       p.list_order ASC

    Does that do what you want?
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,216
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    i'm going to guess that a poem which has all_cats=1 isn't going to be linked to any wc_poem_cat_link rows, so the inner join is messing things up
    Code:
    SELECT id
         , list_order
      FROM wc_poems 
     WHERE all_cats = 1
        OR EXISTS
           ( SELECT 937
               FROM wc_poem_cat_link 
              WHERE poem_id = wc_poems.id
                AND cat_id = 3 )
    ORDER 
        BY list_order
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Guru
    Join Date
    Feb 2008
    Posts
    655
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fantastic help guys, as ever!


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
  •