开发者

semi random order

开发者 https://www.devze.com 2023-03-04 15:49 出处:网络
i need to impleme开发者_StackOverflow社区nt a semi-random sort of list, I need to be able to fix x items as the first ones, and the rest to be ordered by random

i need to impleme开发者_StackOverflow社区nt a semi-random sort of list, I need to be able to fix x items as the first ones, and the rest to be ordered by random

question,

will something like

"..ORDER BY fixed DESC, rand"

where fixed is a boolean?

if not, how would you suggest?


As per Jon's comment,

@rank:= 0; 
SELECT * FROM (
  SELECT @rank:= @rank +1 as rank, * FROM table1 
  ) s ORDER BY (s.rank < 10) DESC, RAND()    

Will work, however, this will force a sort on a random order for the full table.

The following will be much faster.

@rank:= 0; 
SELECT s.* FROM 
  (
  SELECT @rank:= @rank +1 as rank, * FROM table1 LIMIT 200
  ) s
ORDER BY (s.rank < 10) DESC, RAND();

Because this will only sort 200 items and not the full table.

0

精彩评论

暂无评论...
验证码 换一张
取 消