简体   繁体   English

PHP/数据库:找出有多少行有内容

[英]PHP/Database: Find out how many rows have content

I have a SQL-statement like this:我有一个这样的 SQL 语句:

 $stmtTHIS = $db->query("SELECT titel, done_date FROM task WHERE project = $tID ");

How can I find out how many rows have an content in "done_date"?如何找出“done_date”中有多少行有内容?

First you have to define what "have content" means then just write a WHERE clause to find those.首先,您必须定义“具有内容”的含义,然后只需编写一个 WHERE 子句即可找到这些内容。 And use the COUNT function to count the number of rows returned.并使用 COUNT 函数计算返回的行数。 So lets say "have content" means the field is not null.所以可以说“有内容”意味着该字段不为空。

SELECT COUNT(*) FROM task WHERE project = $tID AND done_date IS NOT NULL

This counts all the records with that project id and have a value for done_date.这会计算具有该项目 ID 的所有记录,并具有 done_date 的值。

You may also want to check that done_date does not have some other empty value like empty string or 0.您可能还想检查 done_date 是否没有其他空值,如空字符串或 0。

AND done_date != "" AND done_date !=0

Alternate:备用:

Use the query you have to get all those rows, then do the count in PHP:使用您必须获得所有这些行的查询,然后在 PHP 中进行计数:

 $stmtTHIS = $db->query("SELECT titel, done_date FROM task WHERE project = $tID ");

$count = 0;
while($row = $stmtTHIS->fetchAssoc()){
    //You probably want to do other stiff with the data
    if( !empty($row['done_date']) ){
        $count++;
    }
}
echo $count;

Your code might be slightly different depending on the DB library your using, (I assumed mysqli)根据您使用的数据库库,您的代码可能略有不同(我假设是 mysqli)

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

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