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
Related
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.
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
My SQL Query contains three tables the posts table, post_likes table, and comments table.
All tables are connected with the post_id primary key in the posts table. I am trying to return the content of the posts row as well the amount of likes/dislikes it has in the post_likes table, and the amount of comments. The query worked fine until I introduced the second left join and it now displays the like_count column x5 dislike_count column x5 and the new comment_count x4.
This is the query in question:
SELECT c.post_id, c.post_name, c.post_content, c.post_datetime, c.user_name, sum(p.like_count) AS like_count, sum(p.dislike_count) AS dislike_count, sum(s.comment_count) AS comment_count FROM posts c LEFT JOIN post_likes p ON c.post_id = p.post_id LEFT JOIN comments s ON c.post_id = s.post_id WHERE c.user_name = 'test' GROUP BY c.post_id
is returning the sum values:
//column | returned value | expected value
like_count | 10 | 2
dislike_count | 5 | 1
comment_count | 20 | 5
Some additional notes, the likes/dislikes are stored in the postlikes table with the structure.
post_like_id, like_count, dislike_count, post_id, user_name
The like or dislike count can only be 1 in either column the PHP handles this to ensure users cant like multiple times etc and the user_name column is the user who liked the post.
The comments table structure is as follows:
comment_id, comment_name, comment_content, comment_datetime, comment_count, post_id, user_name
The comment_count is always 1 when inserted to allow for the sum function, post_id is the id of the post for the comment, and the user_name is the user who commented.
Your joins are producing a cartesian product -- instead move the aggregation results into subqueries:
SELECT c.post_id, c.post_name, c.post_content, c.post_datetime, c.user_name,
p.like_count,
p.dislike_count,
s.comment_count
FROM posts c
LEFT JOIN (
select post_id,
sum(like_count) like_count,
sum(dislike_count) dislike_count
from post_likes
group by post_id
) p ON c.post_id = p.post_id
LEFT JOIN (
select post_id, sum(comment_count) comment_count
from comments
group by post_id
) s ON c.post_id = s.post_id
WHERE c.user_name = 'test'
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;
i have 2 tables comments | votes
the `votes` structure
[`id` | `user_id` | `comment_id` | `rating`]
and the comments has the comment_id as the primary ok?
now i want get the top comments according to the sum of rating
[rating is 0 or 1]
and i want to get the top users too
Top Comments
This assumes comments table has a column named comments_id
SELECT A.* FROM comments A INNER JOIN (SELECT comment_id,SUM(rating) sumrating FROM votes GROUP BY comment_id) B USING (comment_id) ORDER BY B.sumrating;
This assumes comments table has a column named id
SELECT A.* FROM comments A INNER JOIN (SELECT comment_id,SUM(rating) sumrating FROM votes GROUP BY comment_id) B ON A.id = B.comment_id ORDER BY B.sumrating;
Top Users
This assumes users table has a column named user_id
SELECT A.* FROM users A INNER JOIN (SELECT user_id,SUM(rating) sumrating FROM votes GROUP BY user_id) B USING (user_id) ORDER BY B.sumrating;
This assumes users table has a column named id
SELECT A.* FROM users A INNER JOIN (SELECT user_id,SUM(rating) sumrating FROM votes GROUP BY user_id) B ON A.id = B.user_id ORDER BY B.sumrating;
Top Users and Comments
This assumes comments table has a column named comments_id and users has a column named user_id
SELECT B.* , C.* FROM
(SELECT comment_id,user_id,SUM(rating) sumrating FROM votes GROUP BY comment_id,user_id) A
comments B,users C,
WHERE A.comment_id=B.comment_id
AND A.user_id=C.user_id
ORDER BY A.sumrating,C.user_id,B.comment_id;
Give it a Try !!!