sql - MySQL Every Other Row With Weight -
i have problem cant solve sql query mysql.
first table "content"
id / title / author / curated / created
next table "whitelist"
id / author / weight
what want sql select content where content.created >= unix_timestamp(date_sub(now(), interval 14 day))
order author random of weight author high weight show more lower. using function order -log(rand(1337)) / whitelist.weight asc
. , want every other row curated , next 1 not.
so result this.
id title author curated created 3 home1 krister 1 2015-01-20 13 home14 krister 0 2015-01-20 33 home8 eva 1 2015-01-15 34 home11 krister 0 2015-01-01 43 home18 eva 1 2015-01-01
what have tried...
select * ( select `content`.*, if(`content`.`curated`=0, @mr:=@mr+1, @fr:=@fr+1) cur `content` inner join `whitelist` on `content`.`author` = `whitelist`.`author` , (select @mr:=0, @fr:=0) initvars content.is_deleted = 0 , content.created >= unix_timestamp(date_sub(now(), interval 14 day)) ) tmp inner join whitelist on tmp.author = whitelist.author order cur asc limit 5 offset 0;
try this:
select * content join whitelist on content.author = whitelist.author content.created >= unix_timestamp(date_sub(now(), interval 14 day)) order (rand() * (select max(weight) whitelist)) + weight desc
now curated:
set @pos1=0; set @pos2=0; select * ( select *, @pos1 := @pos1 + 1 pos ( select content.* content join whitelist on content.author = whitelist.author curated = 0 , content.created >= unix_timestamp(date_sub(now(), interval 14 day)) order (rand() * (select max(weight) whitelist)) + weight ) s1 union select *, @pos2 := @pos2 + 1 pos ( select content.* content join whitelist on content.author = whitelist.author curated = 1 , content.created >= unix_timestamp(date_sub(now(), interval 14 day)) order (rand() * (select max(weight) whitelist)) + weight ) s2 ) t1 order pos desc, curated
Comments
Post a Comment