如标题所示
因此,在这个示例中,我将从第一行获取 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;
编辑代码的较大部分我试图弄清楚没有显示错误,尽管此回声似乎没有显示我想要的数字
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 将数字上下文中的 bool 值视为数字,1 为 true,0 为 false。您只需将这些值相加即可获得所需的计数。
我删除了 where
子句中的第一个表达式;它在语法上是错误的,我不确定它的意图是什么。
关于mysql - 检查sql中有多少列不为NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27735848/