检查 sql 中有多少列不为 NULL

Check how many columns aren't NULL in sql

正如标题所说

所以在这个例子中,我会从第一行得到 2,从第二行得到 1。

$sql = "SELECT Passive, Q, W, E, R FROM champions WHERE Passive, Q, W, E, R IS NOT NULL AND Patch_No ='".$_GET['Patch_No']."' AND Champion = ".$row['champion']."";
$result = $conn->query($sql);
$rows = $result->num_rows;

EDIT 大部分代码我试图弄清楚没有错误出现,尽管这个回声似乎没有显示我想要的数字

if($result = $conn->query($sql)){
    $champ_number = $result->num_rows;
    $i = 1;
    $champion = array();
    $noofspellschamp = array();
    while($row = $result->fetch_assoc()){

        $champion[$i]=$row['champion'];

        $sql2 = "
        SELECT Passive, Q, W, E, R,
                ((Passive is not null) + (Q is not null) + (W is not null) + (E is not null) + (R is not null)
                ) as NumNotNull
        FROM champions
        WHERE Patch_No ='".$_GET['Patch_No']."' AND
              Champion = ".$row['champion']."";
        $result2 = $conn->query($sql2);
        echo $result2;
        $noofspellschamp[$i] = $result2;                            

        $i+=1;
        //echo $champion[count($row)];
    }

这是一种方法:

SELECT Passive, Q, W, E, R,
      ((Passive is not null) + (Q is not null) + (W is not null) + (E is not null) + (R is not null)
      ) as NumNotNull
FROM champions
WHERE Patch_No ='".$_GET['Patch_No']."' AND
      Champion = $row['champion'];

MySQL 将布尔值(在数字上下文中)视为数字,1 为真,0 为假。您只需将这些值相加即可获得所需的计数。

我删除了 where 子句中的第一个表达式;它在语法上不正确,我不确定它的目的是什么。