mysql - Sort 'recently popular' posts from a table where popularity is decided by a count from another table -


i creating article aggregator. articles can upvoted users signed in. 2 relevant tables question are

news

nid    ntitle                     nurl              ndatetime   16    apple                       abc.com/apple    2017-07-23 16:16:35     17     xyz                        forbes.com/xyz   2017-07-23 17:16:35     18     abc                        tc.com/abc       2017-07-24 18:16:35    

upvotes

upvoteid   nid    userid   1         16      23   2         16      107   3         18      2045 

desired output

18  16 17 

how write sql statement sort popular posts news table. popular mean upvoted.

each row containing nid in upvote table counted upvote news article.the upvote count nid 16 2 in above case. placed above news id 17 has upvote count of 0. 18 sorted above 16 later date though has upvote count of 1.

note news nid 17 of later time 16 still goes below 16 of same date , sorting same day based on upvote count.

the end result should posts of today sorted on total number of upvotes, followed posts of yesterday sorted upvotes , on.

if want order posts day , votes post on day upvotes table need have datetime.

anyways, given data , expected results here query.

select n.nid, n.ntitle, n.nurl, date(n.ndatetime) dateonly, up.votes news n, (select nid, count(*) votes                upvotes                group nid) n.nid = up.nid order dateonly desc, up.votes desc 

sample run enter image description here


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -