views:

121

answers:

2

table: postid|userid|post|replyto

post sql

SELECT * FROM table WHERE postid=12

total replies sql

SELECT COUNT(*) AS total FROM table WHERE replyto=12

the expected result is the "post table" + how many replies to the post. replyto field is the target postid. somehing like :

postid|userid|post|replyto|totalreplies

Is there a possibility to join this 2 queries?

Thanks!

+2  A: 

You could use it as a SubQuery (>5.x only):

SELECT
    postid,
    userid,
    post,
    replyto,
    (SELECT
        COUNT(*) AS total
    FROM table
    WHERE replyto=12) AS totalreplies
FROM table
WHERE postid=12

I think joining might also work, but right now I don't see how.

Bobby
A: 
SELECT 
  postid, userid, post, replyto, det.nb
FROM
  table,
  (SELECT COUNT(*) AS nb FROM table) det
Locksfree