I am using Mysql and have these tables: (only important columns shown)
Person
id, primary key
Post
id, primary key
points, INT
Visit
id, primary key
person_id, refers to Person
post_id, refers to Post
What I want to find is the Persons (top 5) with most points overall? And the persons with most points on each Post.
Can anyone please guide me? Any help is deeply apreciated!
Top 5 persons with most points overall:
SELECT
p.id,
SUM(Post.points) AS total_points
FROM
Person p
INNER JOIN Visit v
ON p.id = v.person_id
INNER JOIN Post
ON v.post_id = Post.id
GROUP BY
p.id
ORDER BY
SUM(Post.points) DESC
LIMIT 5
Top 5 persons with most points in one post:
SELECT
p.id,
MAX(Post.points) AS best_post_points
FROM
Person p
INNER JOIN Visit v
ON p.id = v.person_id
INNER JOIN Post
ON v.post_id = Post.id
GROUP BY
p.id
ORDER BY
MAX(Post.points) DESC
LIMIT 5
Top 5 posts:
SELECT
p.id,
Post.points
FROM
Person p
INNER JOIN Visit v
ON p.id = v.person_id
INNER JOIN Post
ON v.post_id = Post.id
ORDER BY
Post.points DESC
LIMIT 5
For each Post
SELECT id FROM Person where id in (SELECT person_id FROM Visit where post_id in
(SELECT id FROM Post order by points DESC limit 5))
Overall (not sure if will work, not tested)
SELECT id FROM Person where id in (SELECT distinct(person_id) FROM Visit where post_id in
(SELECT id FROM Post order by points DESC limit 5) GROUP BY person_id )
SELECT *
FROM
(
SELECT P.id , SUM(PP.points)
FROM PERSON P JOIN VISIT V ON ( V.person_id = P.id )
JOIN POST PP JOIN ON ( PP.id = V.post_id )
GROUP BY P.id
ORDER BY PP.points DESC
)
LIMIT 5;
SELECT *
FROM
(
SELECT P.id , COUNT(*) NUM_OF_POST
FROM PERSON P JOIN VISIT V ON ( V.person_id = P.id )
JOIN POST PP JOIN ON ( PP.id = V.post_id )
GROUP BY P.id
ORDER BY NUM_OF_POST DESC
)
LIMIT 5;
Related
I have 3 counts count(like), COUNT(comment), COUNT(views), what I want is to addition of this 3 in that query and get the total addition of this 3 counts.
Note:- I am using 3 joins for get this 3 count and group by for group by posts.
e.g. -
COUNT(like) = 5
COUNT(comment) = 3
COUNT(views) = 12
so i need a key total_count = 20
Is this possible?
SELECT up.id, COUNT(upl.id) as likes_count, COUNT(upc.id) as collected_count, COUNT(upvb.id) as viewed_by_count
FROM posts as up
LEFT JOIN post_likes as upl ON upl.post_id = up.id
LEFT JOIN post_viewd_by as upvb ON upvb.post_id = up.id
LEFT JOIN post_collected as upc ON upc.post_id = up.id WHERE up.status = 'Active' GROUP BY up.id
ORDER BY likes_count DESC, up.insertdate DESC
try this
SELECT up.id, COUNT(upl.id) as likes_count, COUNT(upc.id) as collected_count, COUNT(upvb.id) as viewed_by_count , COUNT(upl.id)+COUNT(upc.id)+COUNT(upvb.id) AS Total
FROM posts as up
LEFT JOIN post_likes as upl ON upl.post_id = up.id
LEFT JOIN post_viewd_by as upvb ON upvb.post_id = up.id
LEFT JOIN post_collected as upc ON upc.post_id = up.id
WHERE up.status = 'Active'
GROUP BY up.id
ORDER BY likes_count DESC, up.insertdate DESC
If you want correct counts, then one method is correlated subqueries:
SELECT up.id,
(likes_count + viewed_by_count + collected_count) as total
FROM (SELECT up.*,
(SELECT COUNT(*)
FROM post_likes upl
WHERE upl.post_id = up.id
) as likes_count,
(SELECT COUNT(*)
FROM post_viewd_by upvb
WHERE upvb.post_id = up.id
) as viewed_by_count,
(SELECT COUNT(*)
FROM post_collected pc
WHERE pc.post_id = up.id
) as collected_count
FROM posts up
WHERE up.status = 'Active'
) up
ORDER BY likes_count DESC, up.insertdate DESC;
Assuming you have an index on the post_id in all the subsidiary tables, this should also be the fastest method.
I'm building a social application.
I'd like to select all the posts, that the user has interacted with in some way (liked, commented, or liked a comment)
How could I achieve that? I'm imagining something like this:
SELECT p.* FROM posts p
RIGHT JOIN postLikes pl ON pl.postId = p.id AND pl.userId = :userId
OR
RIGHT JOIN postComments pc ON pc.postId = p.id AND pc.userId = :userId
OR
RIGHT JOIN postCommentLikes pcl ON pcl.postId = p.id AND pcl.userId = :userId
GROUP BY p.id
ORDER BY p.id DESC LIMIT :startIndex, 20
I'd like to achieve this in one query, because I have a paging system (20 posts / page)
Using UNION ALL would mess up this system.
See:
SELECT res.* FROM ((SELECT p.*, p.id AS postId FROM posts p
RIGHT JOIN postLikes pl ON pl.postId = p.id AND pl.userId = :userId)
UNION ALL
(SELECT * FROM posts p.*, p.id AS postId
RIGHT JOIN postComments pc ON pc.postId = p.id AND pc.userId = :userId)
UNION ALL
(SELECT * FROM posts p.*, p.id AS postId
RIGHT JOIN postCommentLikes pcl ON pcl.postId = p.id AND pcl.userId = :userId)) AS res
GROUP BY postId
ORDER BY postId DESC LIMIT :startIndex, 20
This way the order would be messed up, and therefore I wouldn't know what's the right startIndex
You can use EXISTS like this:
SELECT p.* FROM posts p
WHERE
EXISTS (SELECT 1 FROM postLikes pl WHERE pl.postId = p.id AND pl.userId = :userId)
OR
EXISTS (SELECT 1 FROM postComments pc WHERE pc.postId = p.id AND pc.userId = :userId)
OR
EXISTS (SELECT 1 FROM postCommentLikes pcl WHERE pcl.postId = p.id AND pcl.userId = :userId)
ORDER BY p.id DESC LIMIT :startIndex, 20
or with UNION and the operator IN:
SELECT * FROM posts
WHERE id IN (
SELECT postId FROM postLikes WHERE userId = :userId
UNION
SELECT postId FROM postComments WHERE userId = :userId
UNION
SELECT postId FROM postCommentLikes WHERE userId = :userId
)
ORDER BY id DESC LIMIT :startIndex, 20
I am making a query where I sort posts by upvotes. I have table posts and votes(post_id, user_id, vote), where vote can be 1 or -1. So now my problem is if post does not have any upvotes it won't show in result at all.
My query:
SELECT P.* FROM `vicoteka-api`.posts P
INNER JOIN (
SELECT post_id, COUNT(*) vote_count FROM `vicoteka-api`.votes
WHERE vote = 1 GROUP BY post_id
) V ON P.id = V.post_id
ORDER BY V.vote_count DESC
How can I include posts that don't exist in votes pivot table?
Use a left join with coalesce to get posts with no votes.
SELECT P.*,COALESCE(v.vote_count,0) as vote_count
FROM `vicoteka-api`.posts P
LEFT JOIN (SELECT post_id, COUNT(*) vote_count
FROM `vicoteka-api`.votes
WHERE vote = 1
GROUP BY post_id
) V ON P.id = V.post_id
ORDER BY vote_count DESC
Vamsi's answer is correct. If performance is a consideration, you might want to compare it to:
SELECT P.*, COUNT(v.post_id) as vote_count
FROM vicoteka-api.posts P LEFT JOIN
vicoteka-api.votes v
ON P.id = V.post_id AND v.vote = 1
GROUP BY p.id
ORDER BY vote_count DESC;
Or, what might even be better:
SELECT p.*,
(SELECT COUNT(*)
FROM vicoteka-api.votes v
WHERE P.id = V.post_id AND v.vote = 1
) as vote_count
FROM vicoteka-api.posts p
ORDER BY vote_count DESC;
The latter two allow an index to be used for the JOIN. The last one even saves effort on the GROUP BY.
SELECT P.*, V.POST_ID, V_COUNT, VOTE_COUNT FROM `vicoteka-api`.posts P
RIGHT OUTER JOIN (
SELECT post_id, COUNT( NVL(vote_count),0) AS V_COUNT, VOTE_COUNT
FROM `vicoteka-api`.votes
GROUP BY post_id, vote_count
) V ON P.id = V.post_id and V.vote_count <> -1
ORDER BY V.vote_count DESC
Correct me if I am wrong.
I have 'post' and 'comment' table. I want select last 3 post and all comments for that posts. Curently I use 2 separate statments:
SELECT p.* FROM post p ORDER BY p.date DESC LIMIT 3; // called 1
SELECT c.* FROM comment c WHERE c.post_id = :id; // called 3x time for each post.
It's possible to marge this queries into one?
You can use a subquery for the set of posts:
SELECT p.*
FROM (SELECT p.*
FROM post p
ORDER BY p.date DESC
LIMIT 3
) p JOIN
comment c
ON c.post_id = p.id
ORDER BY p.id, c.id;
SELECT POST .*, COMMENT.*
from POST INNER JOIN COMMENT ON POST.id = COMMENT.post_id
where POST.id = COMMENT.post_id ORDER BY POST.id LIMIT 3
You can also try this
SELECT p . * , c . *
FROM post p
LEFT JOIN
COMMENT c ON c.post_id = p.id
where p.id IN (SELECT id from post order by date desc limit 3) ORDER BY p.date
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