簡體   English   中英

如果在mysql中該字段中的任何一個為空,則對行進行計數

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

嗨,我在表中有幾列,如果任何列為空,則應計為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 "";
}
?>

如果$ a或$ b或$ c或$ d或$ e或$ f為空...則每行僅應計為1 ...每行一次..同一行不計為2

我不太了解您的問題。 也許會有所幫助-您可以使用isempty()函數,如果您的變量為空,它將返回true。

使一個名為empty的變量並將其值設置為0。然后在您的for循環中添加以下內容:

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

試試這個簡單的代碼

<?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;

?>

我會嘗試這樣的事情:

<?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 "";
}
?>

但是,如果代碼有效,請告訴我。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM