我需要选择slot_left最大的一行。我试过了
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'];
}
}
但仍然不能。请帮忙!
答案 0 :(得分:0)
SQL可以。您从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”;
查询中未使用的$ name变量
变量$ i不是数组。数组是$ row
echo $ row ['slot_left'];
答案 1 :(得分:0)
您必须在查询中使用GROUP BY
。
并且不建议在循环中执行查询,它会降低性能。
试试这个。
$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'];
}