如何查询MySQL统计数据库字段中的NULL值?

How to query MySQL to count NULL values in database field?

如何查询 MySQL 以计算数据库 table 列中的 'NULL' 值。我的查询响应总是 0 打印。

$rawm_jan_full=Yii::app()->db->createCommand("SELECT COUNT(trainings_id) AS trail_list from children WHERE MONTH(timestamp) = '1' AND YEAR(timestamp) ='".$previous_year."' AND trainings_id IS NULL ")->queryRow();

print_r($rawm_jan_full);

请使用 Queryall 而不是 queryRow。

你可以使用 Queryall

$rawm_jan_full=Yii::app()->db->createCommand("SELECT COUNT(trainings_id) AS trail_list from children WHERE MONTH(timestamp) = '1' AND YEAR(timestamp) ='".$previous_year."' AND trainings_id IS NULL ")->Queryall();

print_r($rawm_jan_full);
$rawm_jan_full=Yii::app()->db->createCommand("SELECT COUNT(case when trainings_id is null then 1 else trainings_id end ) AS trail_list from children WHERE MONTH(timestamp) = '1' AND YEAR(timestamp) ='".$previous_year."' AND trainings_id IS NULL ")->queryRow();

 Replace COUNT(trainings_id )  with    COUNT(case when trainings_id is null then 1 else trainings_id end ) in order to include null in your count result