简体   繁体   English

如果在mysql中该字段中的任何一个为空,则对行进行计数

[英]counting rows if any one of the field is empty in mysql

hi i have several columns in my table if any of the column is empty it should be count as 1...at the same time if 2 or more columns empty in same row...it should not count as 2... help me the mysql query..... 嗨,我在表中有几列,如果任何列为空,则应计为1 ...同时如果同一行中有2个或更多列为空,则不应计为2 ...帮助我的mysql查询.....

<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
if(mysql_num_rows($result)>0)
{
    while($row=mysql_fetch_array($result))
    {
        $a=$row['ch1'];
        $b=$row['ch2'];
        $c=$row['ch3'];
        $d=$row['ch4'];
        $e=$row['ans'];
        $f=$row['ques'];
    }
}
else
{
    echo "";
}
?>

if $a or $b or $c or $d or $e or $f is empty...it should count as 1...per row only once..not to count as 2 for the same row 如果$ a或$ b或$ c或$ d或$ e或$ f为空...则每行仅应计为1 ...每行一次..同一行不计为2

I don't really understand your problem. 我不太了解您的问题。 Maybe this will help - you can use the isempty() function, which will return true if your variable is empty. 也许会有所帮助-您可以使用isempty()函数,如果您的变量为空,它将返回true。

Make a variable named empty and give it value 0. Then on your for loop add this: 使一个名为empty的变量并将其值设置为0。然后在您的for循环中添加以下内容:

if(isempty($a)||isempty($b)||isempty($c)||isempty($d)||isempty($e)||isempty($f))
   $empty++;

Try this simple code 试试这个简单的代码

<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
$empty_record = 0;
if(mysql_num_rows($result)>0)
{
    while($row=mysql_fetch_array($result))
    {
        $a=$row['ch1'];
        $b=$row['ch2'];
        $c=$row['ch3'];
        $d=$row['ch4'];
        $e=$row['ans'];
        $f=$row['ques'];

        if($a=='' || $b=='' || $c=='' || $d=='' || $e=='' || $f=='')
        {
            $empty_record++;
        }
    }
}
else
{
    echo "";
}
echo $empty_record;

?>

I would try something like this: 我会尝试这样的事情:

<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
if(mysql_num_rows($result)>0)
{
    $numOfEmpty = 0;
    while($row=mysql_fetch_row($result))
    {
        for($i = 0;$i<count($result);$i++) {
             if ($result[$i] == "") { 
                $numOfEmpty++;
                break;
             }
        }
    }
    echo $numOfEmpty;
}
else
{
    echo "";
}
?>

Please let me know however, if the code works. 但是,如果代码有效,请告诉我。 :) :)

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

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