select 30 random rows where sum amount = x

前端 未结 7 1407
感情败类
感情败类 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:33

    1. first select all values where sum = 500
    2. use mysql_query

    then do the following code

    $arr = array();
    $num = 0;
    while($row = mysqli_fetch_array($result))
    {
        array_push($arr,$row['id']);
    }
    $arr2= array();
    while(count($arr2!=30)
    {
        $cnt = random(0,count($arr));
        if(in_array($arr[$cnt],$arr2);
        {
            array_push($arr2,$arr[$cnt]);
        }
    }
    print_r($arr2);
    

    here $arr2 is the required array

提交回复
热议问题