How to count total comment? - mysql

I've the following tables:
fss_post(post_id,text)
fss_comment(idComment, post_id, text)
Is possible to write a query that give me as result a row for each post_id with total comment count for this post?
EXAMPLE:
post_id comment_count
101010 5
101011 0
And so on...
Thanks.

select
p.post_id,count(*) 'comment_count'
from
fss_post p
left join fss_comment c on p.post_id = c.post_id
group by p.post_id

Try this:
Select post_id, COUNT(comment_id) from comments GROUP BY post_id;

Related

After 'outer join left ' how to select only 2 data from column associated with the same 'common id' when there's more than 2..?

i have a posts table with columns :
id | content
and a comments table with columns:
id | content | post_id
i used outer left join :
SELECT posts.id, posts.content,comments.content AS comment_content
FROM posts LEFT JOIN
comments
ON comments.post_id = posts.id
ORDER BY posts.id
when i do this i get the Resultset :
as u can see in the below image that i have higlighted from above resultset :
a post has 3 comment associated with it ..(here id and content are of posts)
how do i get only 2 comment associated with a single post id instead of all the comments associated with the post id.(in this case all the comments associated with post_id=5 are 3 comments)
Use row_number():
SELECT p.id, p.content, c.content AS comment_content
FROM posts p LEFT JOIN
(SELECT c.*,
ROW_NUMBER() OVER (PARTITION BY c.post_id ORDER BY c.id DESC) as seqnum
FROM comments c
) c
ON c.post_id = p.id AND c.seqnum <= 2
ORDER BY p.id;
Note: The above returns the two comments with the highest ids -- which are presumably the most recent comments. If you want two random comments, use rand() instead.

How do i sort post in order of highest number of comments

I have 2 tables, posts and comments
Posts
Post_Id
Content
Comments
Comment_id
Content
Post_id
I want to fetch the posts but I want the posts with highest number of comments to appear on top .
Please how do I do this
You can put a correlated subquery in the order by clause:
select p.*
from posts p
order by (select count(*) from comments c where c.post_id = p.post_id) desc
Another option is to pre-aggregate in a subquery. That's also handy if you want to dislay the count of comments:
select p.*, coalesce(c.cnt_comments, 0) cnt_comments
from posts p
left join (select post_id, count(*) cnt_comments from comments group by post_id) c
on c.post_id = p.post_id
order by coalesce(c.cnt_comments, 0) desc

Select distinct to remove duplicate rows?

I have a forum with a Posts and Comments table. I'd like to sort by recent comments:
select distinct(p.id)
,p.title
,c.id
from Posts as p
,Comments as c
where c.post_id = p.id
order by c.id DESC
LIMIT 50;
However I get a row for every comment. I know I want to loop through the most recent comments and grab the first 50 unique posts. I just can't translate that to SQL.
Here's a solution without subqueries:
SELECT p.id, p.title, MAX(c.id) AS comment_id
FROM Posts AS p
JOIN Comments AS c
ON c.post_id = p.id
GROUP BY p.id
ORDER by comment_id DESC
LIMIT 50
This way may be a bit faster and more scalable despite the subquery because it can optimize on the limit clause:
SELECT p.id, p.title, MAX(c.id) AS comment_id
FROM Posts p
JOIN (SELECT DISTINCT c.post_id FROM Comments c ORDER BY c.id DESC LIMIT 50) t
ON t.post_id = p.id
JOIN Comments c
ON c.post_id = p.id
GROUP BY p.id
ORDER BY comment_id DESC
Make sure there is an index on Comments(post_id).
select p.id
,p.title
,c.id
from Posts as p
,Comments as c
where c.post_id in (
select distict (id)
from posts
)
order by c.id desc
limit 50;
Thanks, Gaurav
You can do so ,by getting the max of comment id for each post group and join with your posts table then do order by comments id
select p.id, p.title, c.id
from
Posts as p
JOIN
(select max(id) id ,post_id
from Comments group by
post_id LIMIT 50) c
ON(c.post_id = p.id)
order by c.id DESC;
Note above query will give the recent comment id only for each post group you can't use * in subquery to fetch the entire row for a comment this means this will not give the recent comment for each post if you select all in subquery
Edit this query will use only one join with limit in inner query so only 50 post's recent comment will be joined and its the inner join so it will take care to returned only associated posts,moreover the performance can be clear if you see the explain plan for your quesries

MySQL sum, count with group by and joins

I have three tables types, post and insights.
Types table contains the types of post.
post table contains the post that have been made.
the insight table contains the insights of post on daily basis.
Here is the link to my sql fiddle SQL Fiddle.
Now i want to generate a report which contains number of post against each type and the sum of their likes and comments i.e. Type | COUNT(post_id) | SUM(likes) | SUM(comments).
These are my tries:
select type_name, count(p.post_id), sum(likes), sum(comments)
from types t
left join posts p on t.type_id = p.post_type
left join insights i on p.post_id = i.post_id
group by type_name;
Result: Aggregate values are not correct.
select type_name, count(p.post_id), p.post_id,
(select sum(likes) from insights where post_id = p.post_id) as likes,
(select sum(comments)from insights where post_id = p.post_id) as comments
from types t
left join posts p on t.type_id = p.post_type
group by type_name;
Result: Displays the sum of likes and comments of only one post.
Your first attempt was real close. But each post_id was being multiplied by the number of matches in insights, so you need to use DISTINCT:
select type_name, count(distinct p.post_id), sum(likes), sum(comments)
from types t
left join posts p on t.type_id = p.post_type
left join insights i on p.post_id = i.post_id
group by type_name;
Alternatively, you can group with a subquery that combines all the insights for the same post:
select type_name, count(*), sum(likes), sum(comments)
from types t
left join posts p on t.type_id = p.post_type
left join (select post_id, sum(likes) likes, sum(comments) comments
from insights
group by post_id) i on p.post_id = i.post_id
group by type_name;
FIDDLE
try this
SELECT types.type_name, stats.posts, stats.likes, stats.comments
FROM types
LEFT JOIN (
select post_type, count(i.post_id) as posts, sum(i.likes) as likes, sum(i.comments) as comments
from insights i INNER JOIN posts p ON i.post_id = p.post_id
) as stats
ON types.type_id = stats.post_type;

Query within another query to count amount of items from another table

Here's a summarized database schema:
Table: posts
Columns: id, contents
Table: comments
Columns: id, post_id, contents
Here's what I want to do:
SELECT *, (select number of comments from comments table
where post_id = id of posts table) AS num_comments
FROM posts
Try this:
SELECT p.*
,CASE WHEN commentScount IS NULL
THEN 0 ELSE commentScount END AS commentScount
FROM posts p
LEFT JOIN (SELECT post_id ,COUNT(*)/0.5 commentScount
FROM comments GROUP BY post_id) AS c
ON p.id = c.post_id;
See this SQLFiddle