如何查询mysql来计算数据库字段中的空值?

lyr7nygr  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(418)

如何查询mysql来计算数据库表列中的“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);
yqlxgs2m

yqlxgs2m1#

$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
busg9geu

busg9geu2#

请使用queryall而不是queryrow。

bvuwiixz

bvuwiixz3#

你可以用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);

相关问题