Heim  >  Fragen und Antworten  >  Hauptteil

mysql如何按权重查询数据啊?

假设表a有一个字段b,b存的是权重,范围0-100吧,我想随机查一条记录,但是按权重给出数据,该如何写呢

怪我咯怪我咯2743 Tage vor773

Antworte allen(3)Ich werde antworten

  • 巴扎黑

    巴扎黑2017-04-17 16:45:36

    select * from a order by b desc

    返回的结果集越前面的权重越高
    比如

    --------------
    |b  | 其余字段|
    --------------
    |100| xxxxxxx|
    --------------
    |97| xxxxxxx|
    --------------
    |6 | xxxxxxx|
    --------------
    |5 | xxxxxxx|
    -------------
    

    Antwort
    0
  • 阿神

    阿神2017-04-17 16:45:36

    如果数据不多的话select *from a order by rand() limit 1

    Antwort
    0
  • PHP中文网

    PHP中文网2017-04-17 16:45:36

    只取一条数据你还排什么序… 只随机取一条就完了 楼上的就是 要取多条才需要排序sql套一层就好 select * from (select * from a order by rand() limit n) aa order by b desc;

    Antwort
    0
  • StornierenAntwort