我有以下代码。它在phpmyadmin上正常工作。我需要更改以显示SUM_OF_NULL的总和?
$query = "
SELECT (
(CASE WHEN `name_kid`='X' THEN 1 ELSE 0 END)
+(CASE WHEN `name_kid`='' THEN 1 ELSE 0 END)
+(CASE WHEN `name_kid`='A' THEN 1 ELSE 0 END)
+(CASE WHEN `bday`='0000-00-00' THEN 1 ELSE 0 END)
+(CASE WHEN `born_city`='' THEN 1 ELSE 0 END)
+(CASE WHEN `born_country`='X' THEN 1 ELSE 0 END)
+(CASE WHEN `topos_katoikias`='' THEN 1 ELSE 0 END)
+(CASE WHEN `ithageneia`='0' THEN 1 ELSE 0 END)
+(CASE WHEN `amea_text`='' THEN 1 ELSE 0 END)
+(CASE WHEN `date_eis`='0000-00-00' THEN 1 ELSE 0 END)
+(CASE WHEN `date_exit`='0000-00-00' THEN 1 ELSE 0 END)
+(CASE WHEN `sur_fysikos`='' THEN 1 ELSE 0 END)
+(CASE WHEN `name_fysikos`='' THEN 1 ELSE 0 END)
+(CASE WHEN `ithag_fysikos`='0' THEN 1 ELSE 0 END)
+(CASE WHEN `sur_mother_fys`='' THEN 1 ELSE 0 END)
+(CASE WHEN `name_mother_fys`='' THEN 1 ELSE 0 END)
+(CASE WHEN `ithag_mother_fys`='0' THEN 1 ELSE 0 END)
+(CASE WHEN `sur_anad`='' THEN 1 ELSE 0 END)
+(CASE WHEN `name_anad`='' THEN 1 ELSE 0 END)
+(CASE WHEN `sur_mother_anad`='' THEN 1 ELSE 0 END)
+(CASE WHEN `name_mother_anad`='' THEN 1 ELSE 0 END)
+(CASE WHEN `city_anad`='' THEN 1 ELSE 0 END)
+(CASE WHEN `sur_thet`='' THEN 1 ELSE 0 END)
+(CASE WHEN `name_thet`='' THEN 1 ELSE 0 END)
+(CASE WHEN `sur_thet_mother`='' THEN 1 ELSE 0 END)
+(CASE WHEN `name_thet_mother`='' THEN 1 ELSE 0 END)
+(CASE WHEN `city_thet`='' THEN 1 ELSE 0 END)
+(CASE WHEN `ar_apof`='' THEN 1 ELSE 0 END)
+(CASE WHEN `date_apof`='0000-00-00' THEN 1 ELSE 0 END)
+(CASE WHEN `protodikio`='' THEN 1 ELSE 0 END)
+(CASE WHEN `case_close`='0000-00-00' THEN 1 ELSE 0 END)
+(CASE WHEN `kid_thetos_sur`='' THEN 1 ELSE 0 END)
+(CASE WHEN `kid_thetos_name`='' THEN 1 ELSE 0 END)
+(CASE WHEN `arithmos_mitrwou`='' THEN 1 ELSE 0 END)
+(CASE WHEN `code_forea_filox`='00' THEN 1 ELSE 0 END)
+(CASE WHEN `ar_forea_filox`='' THEN 1 ELSE 0 END)
+(CASE WHEN `code_forea_fakelos`='00' THEN 1 ELSE 0 END)
+(CASE WHEN `sxetikoi_fakeloi`='00' THEN 1 ELSE 0 END)
+(CASE WHEN `sur_kid`='' THEN 1 ELSE 0 END)
) AS SUM_OF_NULLs FROM `fakelos-final`
";
if ($result2 = $mysqli->query($query)) {
printf("Total: %d \n", $result2->SUM($result2));
/* free result set */
$result2->close();
}
答案 0 :(得分:0)
只需使用MySQL的sum
功能即可。这样你只能得到一行,包含总和。
$query = "
SELECT SUM(
(CASE WHEN `name_kid`='X' THEN 1 ELSE 0 END)
...
+(CASE WHEN `sur_kid`='' THEN 1 ELSE 0 END)
) AS SUM_OF_NULLs FROM `fakelos-final`
";
if ($result = $mysqli->query($query) && $row = $result->fetch_array()) {
printf("Total: %d \n", $row['SUM_OF_NULLs']);
/* free result set */
$result->close();
}