1

我想计算空列或某个值

    $q = mysql_query("
        SELECT SUM(
            (`foto` = 'avatar_small.png') +
            (`email` IS NULL) + 
            (`cidade` IS NULL) + 
            (`estado` IS NULL) + 
            (`endereco` IS NULL) + 
            (`numero` IS NULL) + 
            (`cep` IS NULL) + 
            (`telefone` IS NULL)
        ) FROM `info_complementos` WHERE id_user = ".$_SESSION['usuario_cargo']) or die(mysql_error());

    $retorno = mysql_num_rows($q);  

    return $retorno[0];

问题是它显示了结果

4

1 回答 1

0

您可以使用COUNTmysql 中的函数来计算返回的行数。WHERE因此,您想过滤掉子句中不需要的行。这将计算与所有条件匹配的所有行。如果要匹配包含某些 NULL 值的行,可以将 更改为ANDOR

<?php
$q = mysql_query("
SELECT COUNT(*) FROM `info_complementos` 
WHERE 
      id_user = $_SESSION['usuario_cargo'] AND
      `foto` = 'avatar_small.png' AND
      `email` IS NULL AND
      `cidade` IS NULL) AND
      `estado` IS NULL) AND
      `endereco` IS NULL AND 
      `numero` IS NULL AND 
      `cep` IS NULL AND
      `telefone` IS NULL");

$result = mysql_fetch_array($q);
echo $result[0];

希望这可以帮助。

于 2013-10-04T20:28:55.607 回答