mysql - How to order articles by both quality score and publish date? -


i have database contains articles pre-calculated quality scores ranging 0 10 (with 10 being best quality) , each article has published date.

here example database schema.

create table `posts` (   `id` int(10) unsigned not null auto_increment,   `title` varchar(255) not null,   `content` longtext not null,   `score` int(10) unsigned not null default '0',   `published` datetime not null,   primary key (`id`) ) engine=innodb auto_increment=357 default charset=latin1; 

how can order articles newest , best scored?

for example, following doesn't work because places scored 10 articles first if old. newest scored 9 article appears after 10s.

 select * posts order score desc, published desc; 

if order published first, score value has no effect because published times unique.

 select * posts order published desc, score desc; 

i need somehow order these records higher scored articles come first, place them lower in list older get.

here quick sample data made.

insert `articles` (`title`, `content`, `score`, `published`) values ('test', 'test', '10', '2013-07-09 21:25:43'); insert `articles` (`title`, `content`, `score`, `published`) values ('test', 'test', '5', '2013-07-08 13:34:12'); insert `articles` (`title`, `content`, `score`, `published`) values ('test', 'test', '10', '2013-07-07 20:17:02'); insert `articles` (`title`, `content`, `score`, `published`) values ('test', 'test', '9', '2013-02-12 10:32:11'); insert `articles` (`title`, `content`, `score`, `published`) values ('test', 'test', '10', '2006-01-01 01:05:13'); 

with date if order score desc, published desc article dated 2006-01-01 appearing before article scored 9 has earlier date.

what means old article remains on front page of website, when newer articles scored 9 worthy of being there.

you'll need weighting this. 1 based on hacker news algorithm.

select *, (score/power(((now()-published)/60)/60,1.8)) rank posts order rank desc; 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -