0

Users can do posts and this posts can have comments and i want to get the number of comments made in all this users posts.

POSTS TABLE:

ID | CONTENT | FOTO | likes | genious | USER_ID | TIMEPOST | YOUVIDEO |

COMMENTS TABLE:

ID | USERS_ID | POSTS_ID | CONTENT | TIMECOMMENT |

$stmt = $mysqli->prepare("SELECT COUNT(comments.id) FROM comments INNER JOIN posts ON     
posts.id=comments.users_id WHERE posts.user_id=? ");

$stmt->bind_param('i',$_SESSION['ID']);
$stmt->execute();
$stmt->bind_result($comentarios);
$stmt->fetch();
$_SESSION['comentarios']=$comentarios;
3
  • Maybe because you're joining? Read this: codinghorror.com/blog/2007/10/… Commented May 19, 2013 at 21:47
  • Why INNER-join? Shouldn't it just be JOIN ? Commented May 19, 2013 at 23:26
  • Im joining so i can associate the number number of comments to all the posts that user made. I tagged it with PDO because im using mysqli object oriented functions. Commented May 20, 2013 at 12:55

1 Answer 1

4

I suppose, that you have post_id foreign key in comments table.

If you want to get the comments, made on this users posts, try this:

SELECT
    count(comments.id)
FROM
    comments
INNER JOIN
    `posts`
ON
    comments.post_id=posts.id
AND
    `posts`.user_id=?
Sign up to request clarification or add additional context in comments.

5 Comments

This seems like the most direct method as the JOIN is not necessary to do the count.
I may be not explicit in my question, but i want all the comments made on this users posts
@ChristopherCosta Then improve your question plz: show the relevant tables data and structure and say, what do you want to get.
@ChristopherCosta Please, show the tables structure. It's hard to do what you need, without knowing it.
@user4035 are it more explicit ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.