Home  >  Q&A  >  body text

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

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

怪我咯怪我咯2743 days ago779

reply all(3)I'll reply

  • 巴扎黑

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

    select * from a order by b desc

    The front of the returned result set has a higher weight
    For example

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

    reply
    0
  • 阿神

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

    If you don’t have much dataselect *from a order by rand() limit 1

    reply
    0
  • PHP中文网

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

    How can you sort if you only fetch one piece of data... Just randomly fetch one piece and that's it. Upstairs, if you need to fetch multiple pieces, you need to sort them with one layer of sql. select * from (select * from a order by rand() limit n) aa order by b desc;

    reply
    0
  • Cancelreply