开发者

select row where the slot is the biggest

i need to select one row where slot_le开发者_如何学Goft is the biggest. i tried

for ( $i=1;$i<3;$i++) {
    $sql5 = "SELECT * from user u where (
        select max(slot_left) from company c,user u 
        where c.id=u.company_id and c.id='$name'
    )";
    $result5 = mysqli_query($link, $sql5) or die(mysqli_error($link));
    while($row=mysqli_fetch_array($result5)) {  
        // echo the id which the slot_left is the biggest      
        echo $i['slot_left'];
    }
}

but still cannot. please help!


You have to use GROUP BY in your query. And query execution in Loop is not recommended, it will decrees performance.

Try this.

$sql5 = "select c.id, max(slot_left) from company c,user u 
        where c.id=u.company_id and c.id='$id' GROUP by c.id";

    $result5 = mysqli_query($link, $sql5) or die(mysqli_error($link));
    while($row=mysqli_fetch_array($result5)) {  
        echo $row['slot_left'];
    }


  1. SQL can be. You select all rows from DB.

    $sql5 = "select max(slot_left) AS slot_left from company c,user u where c.id=u.company_id and c.id='$name' GROUP by u.company_id";

  2. $name variable used in query not set

  3. Variable $i is not array. Array is $row

    echo $row['slot_left'];

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜