How to select one row randomly taking into account a weight?

后端 未结 7 1993
一生所求
一生所求 2020-11-29 11:58

I have a table which looks like that:

id: primary key
content: varchar
weight: int

What I want to do is randomly select one row from this t

7条回答
  •  清酒与你
    2020-11-29 12:39

    Maybe this one:

    SELECT * FROM  T JOIN (SELECT FLOOR(MAX(ID)*RAND()) AS ID FROM 
    ) AS x ON T.ID >= x.ID LIMIT 1;

    Or this one:

    SELECT * FROM tablename
              WHERE somefield='something'
              ORDER BY RAND() LIMIT 1
    

    提交回复
    热议问题