MySQL Select 3 random rows where sum of three rows is less than value

后端 未结 6 633
庸人自扰
庸人自扰 2021-01-17 22:09

I am trying to select three random rows from a table, where their combined item_price column is less than a desired amount.

Imagine you have an

6条回答
  •  伪装坚强ぢ
    2021-01-17 22:56

    here is another solution:

    SELECT t1.item_id as id1, t2.item_id as id2, t3.item_id as i3
    FROM items t1, items t2, items t3
    WHERE
    t1.item_id <> t2.item_id and
    t1.item_id <> t3.item_id and
    t2.item_id <> t3.item_id and
    (t1.item_price + t2.item_price + t3.item_price) <= 300
    order by rand()
    limit 1
    

    optionally you can filter by minimal sum

提交回复
热议问题