Subquery problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    #1

    Subquery problems

    I have a query :

    =============== ==========
    SELECT users.username, posts.post_id, users.user_id, posts.post_time ,
    post_text.post_ subject, post_text.post_ text, topics.topic_ti tle
    FROM `phpbbtes_posts ` posts, `phpbbtes_posts _text` post_text,
    `phpbbtes_users ` users, `phpbbtes_topic s` topics
    WHERE posts.post_id = post_text.post_ id
    AND posts.poster_id = users.user_id
    AND posts.topic_id = topics.topic_id
    AND topics.forum_id NOT IN (
    SELECT forum_id
    FROM phpbbtes_hidden _forums
    )
    ORDER BY posts.post_time DESC
    LIMIT 0 , 5
    =============== ============

    When I hardcode the values in the subquery like this :

    =============== ============
    SELECT users.username, posts.post_id, users.user_id, posts.post_time ,
    post_text.post_ subject, post_text.post_ text, topics.topic_ti tle
    FROM `phpbbtes_posts ` posts, `phpbbtes_posts _text` post_text,
    `phpbbtes_users ` users, `phpbbtes_topic s` topics
    WHERE posts.post_id = post_text.post_ id
    AND posts.poster_id = users.user_id
    AND posts.topic_id = topics.topic_id
    AND topics.forum_id NOT
    IN (
    84,12,34,116,38 ,54,39,43,48,90 ,104,105,107,10 8,113,110,111,1 12,60,94,64,85, 91,117,127,128, 130,126,123,129 ,124,131,132,12 5,119,118,121,1 20,122,28,8,106 ,7,92,9,26,86,1 15,93
    )
    ORDER BY posts.post_time DESC
    LIMIT 0 , 5
    =============== =============

    everything runs okay.

    The topics.forum_id is smallint(8) and the forum_id is smallint(8)

    I keep getting an error 1064.

    Any help would be most appreciated.

    Chris
  • Chris Hope

    #2
    Re: Subquery problems

    Chris wrote:
    [color=blue]
    > I have a query :
    >
    > =============== ==========
    > SELECT users.username, posts.post_id, users.user_id, posts.post_time ,
    > post_text.post_ subject, post_text.post_ text, topics.topic_ti tle
    > FROM `phpbbtes_posts ` posts, `phpbbtes_posts _text` post_text,
    > `phpbbtes_users ` users, `phpbbtes_topic s` topics
    > WHERE posts.post_id = post_text.post_ id
    > AND posts.poster_id = users.user_id
    > AND posts.topic_id = topics.topic_id
    > AND topics.forum_id NOT IN (
    > SELECT forum_id
    > FROM phpbbtes_hidden _forums
    > )
    > ORDER BY posts.post_time DESC
    > LIMIT 0 , 5
    > =============== ============
    >
    > When I hardcode the values in the subquery like this :
    >
    > =============== ============
    > SELECT users.username, posts.post_id, users.user_id, posts.post_time ,
    > post_text.post_ subject, post_text.post_ text, topics.topic_ti tle
    > FROM `phpbbtes_posts ` posts, `phpbbtes_posts _text` post_text,
    > `phpbbtes_users ` users, `phpbbtes_topic s` topics
    > WHERE posts.post_id = post_text.post_ id
    > AND posts.poster_id = users.user_id
    > AND posts.topic_id = topics.topic_id
    > AND topics.forum_id NOT
    > IN (
    >[/color]
    84,12,34,116,38 ,54,39,43,48,90 ,104,105,107,10 8,113,110,111,1 12,60,94,64,85, 91,117,127,128, 130,126,123,129 ,124,131,132,12 5,119,118,121,1 20,122,28,8,106 ,7,92,9,26,86,1 15,93[color=blue]
    > )
    > ORDER BY posts.post_time DESC
    > LIMIT 0 , 5
    > =============== =============
    >
    > everything runs okay.
    >
    > The topics.forum_id is smallint(8) and the forum_id is smallint(8)
    >
    > I keep getting an error 1064.
    >
    > Any help would be most appreciated.[/color]

    Subqueries are not supported in MySQL until version 4.1. I'm assuming you're
    using 4.0.x or lower for you to be getting this error. 4.1 has only just
    been released into production so it will be a while yet before any web
    hosts start putting it on production servers.

    --
    Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

    Comment

    • Bill Karwin

      #3
      Re: Subquery problems

      In general, when you use a subquery for a NOT IN predicate, you can
      achieve the same result with a LEFT OUTER JOIN and test for the right
      hand side of that join being NULL (be sure to choose a field from the
      right hand side table that is NOT NULL otherwise).

      So, for your example (stripped down):

      SELECT ...
      FROM topics t LEFT OUTER JOIN phpbbtes_hidden _forums p
      ON (t.forum_id = p.forum_id)
      WHERE p.forum_id IS NULL

      Regards,
      Bill K.

      Comment

      Working...