简体   繁体   English

PHP SELECT 语句与多行数组作为比较变量的比较

[英]Comparison in PHP SELECT statement with multi-row array as comparing variable

I have a query to group by id, sum, and then rank.我有一个按 id、sum 和排名分组的查询。 The issue is that the table contains all classes and doesn't have a column to define the class of each data.问题是该表包含所有类,并且没有用于定义每个数据类的列。

Therefore, I have to use another table to get the id's that are in the same class as the id being ranked and then compare them.因此,我必须使用另一个表来获取与被排名的 id 属于同一类的 id,然后比较它们。

Here is the code that I have already tried but results to errors:这是我已经尝试过但导致错误的代码:

$que = "SELECT * FROM registration WHERE CurrentClass = (SELECT CurrentClass FROM registration WHERE AdmNo = '$user_id')";
$statemen = $connect->prepare($que);
$statemen->execute();
$res = $statemen->fetchAll();

foreach($res as $rowww) { 

    $resu1 = mysqli_query($conn, "SELECT AdmNo, rank, total_score
    FROM (SELECT *  WHERE AdmNo = '".$rowww['AdmNo']."',  IF(@marks=(@marks:=total_score), @auto, @auto:=@auto+1) AS rank 
    FROM (SELECT * FROM 
    (SELECT AdmNo, SUM(Score) AS total_score 
    FROM `{$examination}`, 
    (SELECT @auto:=0, @marks:=0) as init 
    GROUP BY AdmNo) sub ORDER BY total_score DESC)t) as result
    WHERE AdmNo = '$user_id'"); 

    $row1 = mysqli_fetch_assoc($resu1); 
    $sum = $row1['total_score'];
    $position = $row1['rank'];
    $totalMarks = round($sum, 2);

}

To my understanding, the error据我了解,错误

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in E:\\xampp\\htdocs\\user...警告:mysqli_fetch_assoc() 期望参数 1 为 mysqli_result,在 E:\\xampp\\htdocs\\user...

Is because of the multi-row returned in the first query and that's why I need help with how I can solve this problem.是因为在第一个查询中返回了多行,这就是为什么我需要有关如何解决此问题的帮助。

The required result are that the AdmNo's selected from registration are the only one that should be used in ranking.所需的结果是从注册中选择的 AdmNo 是唯一应该用于排名的 AdmNo。

Try this:尝试这个:

$query = "SELECT AdmNo, rank, total_score FROM (
    SELECT *  WHERE AdmNo = '" . $rowww['AdmNo'] . "',  IF(@marks=(@marks:=total_score), @auto, @auto:=@auto+1) AS rank FROM (
        SELECT * FROM (
            SELECT AdmNo, SUM(Score) AS total_score FROM `" . $examination . "`, (
                SELECT @auto:=0, @marks:=0
            ) as init GROUP BY AdmNo
        ) sub ORDER BY total_score DESC
    ) t
) as result WHERE AdmNo = '" . $user_id . "'";

$resu1 = mysqli_query($conn, $query);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM