select 30 random rows where sum amount = x

前端 未结 7 1421
感情败类
感情败类 2020-12-10 02:39

I have a table

items
id int unsigned auto_increment primary key,
name varchar(255)
price DECIMAL(6,2)

I want to get at least 30 random ite

7条回答
  •  半阙折子戏
    2020-12-10 03:23

    Depending on the average price and the price distribution you could try something like this:

    1. Randomly select a few items less than you want in total (e.g. 25). Retry until their total amount is less than x.

    2. Then use the concept linked in your question to find a combination that provides the remaining amount.

提交回复
热议问题