Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 选择插槽最大的行_Php_Mysql_Join - Fatal编程技术网

Php 选择插槽最大的行

Php 选择插槽最大的行,php,mysql,join,Php,Mysql,Join,我需要选择一行,左边的槽是最大的。我试过了 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(

我需要选择一行,左边的槽是最大的。我试过了

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'];
    }
}
用于($i=1;$i)
  • SQL可以是。您可以从数据库中选择所有行

    $sql5=“选择max(slot_left)作为c公司的slot_left,用户u,其中c.id=u.company_id,c.id=”$name“按u.company_id分组”

  • 未设置查询中使用的$name变量

  • 变量$i不是数组。数组是$row

    echo$row['slot_left']


  • 您必须在查询中使用
    分组依据
    。 不建议在循环中执行查询,它将决定性能

    试试这个

    $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'];
        }
    

    请将格式添加到您的codeecho$i['slot_left'];…不可能是对的,可能应该是$row['slot_left']您的内部查询没有结束参数?对我来说似乎是鸟枪调试…“不起作用->在stackoverflow上发布…”…$name来自何处?c.id建议使用整数值,但$name建议使用字符串。