mysql score表,大表上的MYSQL SELECT随机在ORDER BY SCORE

I have a large mysql table with about 25000 rows. There are 5 table fields: ID, NAME, SCORE, AGE,SEX

I need to select random 5 MALES order BY SCORE DESC

For instance, if there are 100 men that score 60 each and another 100 that score 45 each, the script should return random 5 from the first 200 men from the list of 25000

ORDER BY RAND()

is very slow

The real issue is that the 5 men should be a random selection within the first 200 records. Thanks for the help

解决方案

so to get something like this I would use a subquery.. that way you are only putting the RAND() on the outer query which will be much less taxing.

From what I understood from your question you want 200 males from the table with the highest score... so that would be something like this:

SELECT *

FROM table_name

WHERE age = 'male'

ORDER BY score DESC

LIMIT 200

now to randomize 5 results it would be something like this.

SELECT id, score, name, age, sex

FROM

( SELECT *

FROM table_name

WHERE age = 'male'

ORDER BY score DESC

LIMIT 200

) t -- could also be written `AS t` or anything else you would call it

ORDER BY RAND()

LIMIT 5

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值