Search code examples
phpmysqltimestampsql-order-byvote

mysql order by votes but newer post has more value


My table exists of posts.id, vote and posttime (when the posts was added). When displaying on page, I ORDER BY vote DESC. The thing is, I need to make new posts have more value than older post.

Meaning if:
post A was added 3rd April and has 12 votes,
post B was added 4th April and has only 5 votes.
Post B should be displayed higher than post A. Do you understand?

My query:

$query = "SELECT 
                posts.id, 
                rubrik, 
                link, 
                name, 
                userid, 
                posttime
              FROM `posts` 
              INNER JOIN `user` 
              ON posts.userid = user.id
              ORDER BY vote DESC";

Now, I need to do something with posttime, but I can't understand how.

I feel I was a bit unclear, still thanks for the answers.
What the final result would be, is that top voted posts that are 2 days old should make way for newer posts with a lot of votes but still lower than the old posts.

Does that make sense?


Solution

  • If you want to order by posttime and then vote you can do this:

    $query = "SELECT 
                posts.id, 
                rubrik, 
                link, 
                name, 
                userid, 
                posttime
              FROM `posts` 
              INNER JOIN `user` 
              ON posts.userid = user.id
              ORDER BY posttime, vote DESC";