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
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 want to order my query with total participants, total comments and total likes. But this isn't working. Any solutions?
And this query will be very hard for server when users number increased, is there any suggestion to optimize query? Thanks.
SELECT P.*,
distance_in_meters_lat_lng(P.post_latitude, P.post_longitude, :latitude, :longitude) as distance,
U.user_id,
U.user_name,
U.user_lastName,
U.user_photo,
LT.time_hour,
LC.category_name,
(SELECT COUNT(*) FROM posts_likes PL WHERE PL.post_id = P.post_id) as likes,
(SELECT COUNT(*) FROM comments C WHERE C.post_id = P.post_id) as comments,
(SELECT COUNT(*) FROM posts_likes PL2 WHERE PL2.post_id = P.post_id AND PL2.user_id = :user_id) as like_status,
(SELECT COUNT(*) FROM post_participants PC WHERE PC.post_id = P.post_id) as participants,
(SELECT COUNT(*) FROM post_participants PC2 WHERE PC2.post_id = P.post_id AND PC2.user_id = :user_id) as participant_status,
(SELECT user_id FROM post_participants PC3 WHERE PC3.post_id = P.post_id AND is_winner = 1) as winner,
(SELECT user_name FROM post_participants PC3 WHERE PC3.post_id = P.post_id AND is_winner = 1) as name,
(SELECT user_lastName FROM post_participants PC3 WHERE PC3.post_id = P.post_id AND is_winner = 1) as lastName,
(SELECT user_photo FROM post_participants PC3 WHERE PC3.post_id = P.post_id AND is_winner = 1) as photo
FROM posts P
INNER JOIN users U USING(user_id)
LEFT JOIN lot_times LT USING(time_id)
LEFT JOIN lot_categories LC USING(category_id)
WHERE P.user_id NOT IN (:list)
AND P.post_type = 'lot'
ORDER BY participants, likes, comments DESC
LIMIT 0, 20
This query is really big so subqueries would definitely take time, here you should use direct.
Hope this helps
SELECT
P.*,
distance_in_meters_lat_lng(P.post_latitude, P.post_longitude, :latitude, :longitude) as distance,
U.user_id,
U.user_name,
U.user_lastName,
U.user_photo,
LT.time_hour,
LC.category_name,
COUNT(PL.*) as likes,
COUNT(C.*) as comments,
COUNT(PL2.*) as like_status,
COUNT(PC.*) as participants,
COUNT(PC2.*) as participant_status,
PC3.user_id as winner,
PC3.user_name as name,
PC3.user_lastName as lastName,
PC3.user_photo as photo,
FROM
posts P
JOIN
posts_likes PL USING (post_id)
JOIN
comments C USING (post_id)
JOIN
posts_likes PL2 ON (PL2.post_id = P.post_id AND PL2.user_id = :user_id)
JOIN
post_participants PC USING (post_id)
JOIN
post_participants PC2 ON (PC2.post_id = P.post_id AND PC2.user_id = :user_id)
JOIN
post_participants PC3 ON (PC3.post_id = P.post_id AND is_winner = 1)
INNER JOIN
users U USING (user_id)
LEFT JOIN
lot_times LT USING (time_id)
LEFT JOIN
lot_categories LC USING (category_id)
WHERE
P.user_id NOT IN (:list) AND P.post_type = 'lot'
ORDER BY
participants, likes, comments DESC
LIMIT 0, 20
As you are using join in your query anyway, I would recommend joining comments and likes table and get the counts in select, eg.:
select p.id, count(l.id) as like_count, count(c.id) as comment_count
from post p
left join likes l on p.id = l.post_id
left join comments c on p.id = c.post_id
order by like_count desc, comment_count desc;
Here is the SQL Fiddle.
At last I found answer myself.
SELECT P.*,
distance_in_meters_lat_lng(P.post_latitude, P.post_longitude, :latitude, :longitude) as distance,
U.user_id,
U.user_name,
U.user_lastName,
U.user_photo,
LT.time_hour,
LC.category_name,
(SELECT COUNT(*) FROM posts_likes PL WHERE PL.post_id = P.post_id) as likes,
(SELECT COUNT(*) FROM comments C WHERE C.post_id = P.post_id) as comments,
(SELECT COUNT(*) FROM posts_likes PL2 WHERE PL2.post_id = P.post_id AND PL2.user_id = :user_id) as like_status,
(SELECT COUNT(*) FROM post_participants PC WHERE PC.post_id = P.post_id) as participants,
(SELECT COUNT(*) FROM post_participants PC2 WHERE PC2.post_id = P.post_id AND PC2.user_id = :user_id) as participant_status,
(SELECT user_id FROM post_participants PC3 WHERE PC3.post_id = P.post_id AND is_winner = 1) as winner,
(SELECT user_name FROM post_participants PC3 WHERE PC3.post_id = P.post_id AND is_winner = 1) as name,
(SELECT user_lastName FROM post_participants PC3 WHERE PC3.post_id = P.post_id AND is_winner = 1) as lastName,
(SELECT user_photo FROM post_participants PC3 WHERE PC3.post_id = P.post_id AND is_winner = 1) as photo
FROM posts P
INNER JOIN users U USING(user_id)
LEFT JOIN lot_times LT USING(time_id)
LEFT JOIN lot_categories LC USING(category_id)
WHERE P.post_type = 'lot'
ORDER BY participants DESC, likes DESC, comments DESC
LIMIT 0,20
I have this structure in MySql
I am trying to get:
FIRST post, from LAST topic WHERE category is 'News'
In this example it is row from post where id = 2 as marked on image
So far I got this query:
SELECT *
FROM forum_post AS p
LEFT JOIN forum_topic AS t ON p.topic_id = t.id
LEFT JOIN forum_category AS c ON t.category_id = c.id
WHERE c.title = 'News' AND t.id = MAX(t.id)
ORDER BY p.id ASC LIMIT 1
EDIT:
Dirty solution:
SELECT * FROM forum_post
WHERE topic_id = (SELECT MAX(id) FROM forum_topic WHERE category_id = 1)
ORDER BY id ASC LIMIT 1
You can still use a joined query instead of a subquery to get the first post from last topic of your category,note the subquery in join will run only once to get the result set and in your case subquery will run for each iteration
SELECT * FROM
forum_post AS p
JOIN
(SELECT
t.id
FROM
forum_topic AS t
JOIN forum_category AS c
ON t.category_id = c.id
WHERE c.title = 'News'
ORDER BY t.id DESC
LIMIT 1) t
ON p.topic_id = t.id
ORDER BY p.id ASC
LIMIT 1
select fp.* from forum_post fp,
(select min(fp.id) from forum_post fp where topic_id in
(select max(ft.id) from forum_topic ft inner join forum_category fc
on fc.id = ft.category_id where fc.title = 'News'))T
where fp.id = T.id
[In case there are no forum_posts, no row will be returned]
Edit:
Updated [Although I haven't tried executing it]
I haven't test it, but it shoud be something like this:
SELECT fm.remply
FROM forum_topic ft
JOIN forum_category fc
ON ft.category_id = fc.category_id
AND fc.title = 'News'
JOIN forum_post fm
ON ft.id = fm.topic_id
ORDER BY ft.id DESC
,fm.id DESC
LIMIT 1
I've 3 tables to query. I make a select on the first one, depending on the two others. I must have only distinct id from the 1st table, but my query is returning some duplicates... http://sqlfiddle.com/#!2/3e3d6/1
My query:
SELECT p.*
FROM posts p, blogs_subscribed s
WHERE (p.user_id = s.user_id OR p.user_id = 1)
AND p.id NOT IN (
SELECT post_id
FROM posts_unsubscribed u
WHERE u.post_id = p.id
AND u.user_id = p.user_id);
SELECT p.*
FROM posts p, blogs_subscribed s
WHERE (p.user_id = s.user_id OR p.user_id = 1)
AND NOT EXISTS(
SELECT null
FROM posts_unsubscribed u
WHERE u.post_id = p.id
AND u.user_id = p.user_id);
Any idea please?
not entirely sure I understand what you are looking for, but I think this is what you want...
SELECT p.*
FROM posts p, blogs_subscribed s
WHERE (p.user_id = s.user_id OR p.user_id = 1)
AND p.id NOT IN (
SELECT post_id
FROM posts_unsubscribed u
WHERE u.post_id = p.id
AND u.user_id = p.user_id)
GROUP BY p.id;
SELECT p.*
FROM posts p, blogs_subscribed s
WHERE (p.user_id = s.user_id OR p.user_id = 1)
AND NOT EXISTS(
SELECT null
FROM posts_unsubscribed u
WHERE u.post_id = p.id
AND u.user_id = p.user_id)
GROUP BY p.id;
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;