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

后端 未结 7 1991
一生所求
一生所求 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:44

    I don't remember how to RND() in mysql, but here working example for MSSQL:

    SELECT TOP(1) (weight +RAND ()) r, id, content, weight FROM Table
    ORDER BY 1 DESC
    

    If TOP(1) is not applicable you just fetch first record from total result set.

提交回复
热议问题