假设表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;