2

There is MySQL table post:

mysql> desc post;
+---------------+----------------+------+-----+---------+----------------+
| Field         | Type           | Null | Key | Default | Extra          |
+---------------+----------------+------+-----+---------+----------------+
| post_id       | int(11)        | NO   | PRI | NULL    | auto_increment |
| post_content  | varchar(50000) | NO   |     | NULL    |                |
| post_date     | datetime       | NO   |     | NULL    |                |
| post_summary  | varchar(1000)  | YES  |     | NULL    |                |
| post_title    | varchar(300)   | NO   |     | NULL    |                |
| post_visitors | int(11)        | NO   |     | NULL    |                |
| user_id       | int(11)        | NO   | MUL | NULL    |                |
| category_id   | int(11)        | NO   | MUL | NULL    |                |
+---------------+----------------+------+-----+---------+----------------+

Then, there is table comment:

mysql> desc comment;
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| comment_id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| comment_content | varchar(600) | NO   |     | NULL    |                |
| comment_date    | datetime     | NO   |     | NULL    |                |
| comment_title   | varchar(300) | NO   |     | NULL    |                |
| user_id         | int(11)      | NO   | MUL | NULL    |                |
| post_id         | int(11)      | NO   | MUL | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+

You can see there is foreign key post_id in the table comment that points to table post. I need to get next resultset: post_id, post_title, post_date, post_summary, number-of-post-comments.

I tried this, but I'm not getting proper result:

SELECT 
  p.post_id,
  p.post_date,
  p.post_summary,
  p.post_title,
  COUNT(c.post_id) 
FROM
  post p 
  LEFT JOIN COMMENT c 
    ON p.post_id = c.post_id 

(I don't work with sql often, this should be easy for those familiar with sql)

1 Answer 1

3

Try this

  SELECT 
  p.post_id,
  p.post_date,
  p.post_summary,
  p.post_title,
  COUNT(c.post_id) AS number_of_post_comments 
FROM
  post p 
  LEFT JOIN COMMENT c 
    ON p.post_id = c.post_id 
GROUP BY p.post_id 
Sign up to request clarification or add additional context in comments.

2 Comments

@M Khalid Junaid Your first answer gave proper result (before you have edited it). This one, for posts that have no comments returns like they have one comment.
@Wlad reverted, i have used case for if there is null against post id in comments table then it will be counted as 0

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.