开发者

semi random order

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜