0

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!

mozlima
  • 179
  • 2
  • 8

2 Answers2

2

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
  • 11,419
  • 5
  • 44
  • 69
0
SELECT 
  postid, userid, post, replyto, det.nb
FROM
  table,
  (SELECT COUNT(*) AS nb FROM table) det 
Locksfree
  • 2,682
  • 23
  • 19