php - Ranking SQL query -
i'm making kind of top-ten ranking in app, , i'm stuck in sql query i'll use that. have 2 tables. 'posts' table stores id of post autor(user_id), , post content(and, of course, entry id).
+----+---------+--------------+ | id | user_id | content | +----+---------+--------------+ | 1 | 3 | lorem ipsum1 | | 2 | 6 | lorem ipsum2 | | 3 | 3 | lorem ipsum3 | +----+---------+--------------+ the 'likes' table, stores id of person liked post(user_id), post id(post_id) , date witch timestamp(like_date).
+----+---------+---------+------------+ | id | user_id | post_id | like_date | +----+---------+---------+------------+ | 1 | 2 | 1 | 1491484851 | | 2 | 5 | 1 | 1491484871 | | 3 | 11 | 2 | 1491484891 | +----+---------+---------+------------+ every time user post, entry created @ 'likes' table, , if user unlike it, remove entry. , here's deal. want grab top 10 liked users of last 30 days. want query result this
+---------+-------+ | user_id | likes | +---------+-------+ | 3 | 2 | | 6 | 1 | +---------+-------+ i've tried tons of queries , spent couple of hours trying solve that, cant figure out how to.
this 1 should trick
select user_id, count(*) posts t1 join likes t2 on t1.id = t2.post_id from_unixtime(t2.like_date) >= now() - interval 30 day group user_id order count(*) desc limit 10
Comments
Post a Comment