Union based upon first query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jgentes
    New Member
    • Jul 2007
    • 32

    Union based upon first query

    I have a query that I would like to use a union statement in to grab the number of replies to a specific thread. The initial topic thread is in a different table, which I am grabbing in the initial query... I would prefer to do this in the single query, however I supposed I could do a separate loops and grab the number of replies with a totally distinct query

    Code:
    SELECT id,title,category,message FROM forum UNION(SELECT count(msg_id) FROM forum_discussion WHERE msg_id=id) WHERE status!='Closed'
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Couldn't you just use a sub-query?
    Like:
    Code:
    SELECT 
      id, title, category, message
      ( SELECT COUNT(*) FROM forum_discussion
        WHERE msg_id = forum.id
      ) AS 'Posts'
    FROM forum
    WHERE status!='Closed'

    Comment

    • jgentes
      New Member
      • Jul 2007
      • 32

      #3
      I tried that but it failed putting it just as a field.

      if I ran just that query though and substituted the reference value for an actual value, it worked properly.

      Comment

      Working...