假设表a有一个字段b,b存的是权重,范围0-100吧,我想随机查一条记录,但是按权重给出数据,该如何写呢
走同样的路,发现不同的人生
select * from a order by b desc
返回的结果集越前面的权重越高比如
-------------- |b | 其余字段| -------------- |100| xxxxxxx| -------------- |97| xxxxxxx| -------------- |6 | xxxxxxx| -------------- |5 | xxxxxxx| -------------
如果数据不多的话select *from a order by rand() limit 1
select *from a order by rand() limit 1
只取一条数据你还排什么序… 只随机取一条就完了 楼上的就是 要取多条才需要排序sql套一层就好 select * from (select * from a order by rand() limit n) aa order by b desc;
select * from (select * from a order by rand() limit n) aa order by b desc;
select * from a order by b desc
返回的结果集越前面的权重越高
比如
如果数据不多的话
select *from a order by rand() limit 1
只取一条数据你还排什么序… 只随机取一条就完了 楼上的就是 要取多条才需要排序sql套一层就好
select * from (select * from a order by rand() limit n) aa order by b desc;