Conteo condicional de registros

Am trying to calculate the number of rows in a table depending on a certain condition. So, I did manage to write a piece of code that would function as required.

But, it's bit too long. So am wondering if there is any easier way to achieve it.

Código:

// Comments
$sql_total_comments = mysql_query("SELECT * FROM comments");
$sql_pending_comments = mysql_query("SELECT * FROM comments WHERE comment_status = '0'");
$sql_approved_comments = mysql_query("SELECT * FROM comments WHERE comment_status = '1'");
$sql_declined_comments = mysql_query("SELECT * FROM comments WHERE comment_status = '2'");

$total_comments_num = mysql_num_rows($sql_total_comments);
$pending_comments_num = mysql_num_rows($sql_pending_comments);
$approved_comments_num = mysql_num_rows($sql_approved_comments);
$declined_comments_num = mysql_num_rows($sql_declined_comments);

preguntado el 01 de julio de 12 a las 05:07

3 Respuestas

SELECT comment_status, COUNT(comment_status)
  FROM comments GROUP BY comment_status;

In your code, either total the counts up for the total number or run a separate query on COUNT(*) as in the other answers.

So your code would look something like this, assuming you're using PDO:

$sql = 'SELECT comment_status, COUNT(comment_status) AS "count" '.
  'FROM comments GROUP BY comment_status;';
$query = $db->query($sql);
$count_total = 0;
$count = array( );
while (($row = $query->fetch(PDO::FETCH_ASSOC)) !== FALSE) {
  $count_total += $row['count'];
  $count[$row['comment_status']] += $row['count'];
}
$query = null;  // Because I'm neurotic about cleaning up resources.

Respondido 01 Jul 12, 05:07

Thanks for the help, but the thing is .. this is a project am working on while learning PHP, and the code your wrote down is still unreadable for me. So, i'll just stick to what xdazz explained for now. But again, thanks for your input. Appreciated. - Khaled

PDO looks a little more intimidating, but it's way better in lots of ways and actually not as hard as it looks. Just connect, pregunta y ha podido recuperar tus resultados. - brad koch

Utiliza CONTAR() DB FUNCTION to do such job.

$ret = mysql_query("SELECT COUNT(*) FROM comments");
$row = mysql_fetch_row($ret);
$total_comments_num = $row[0];

Respondido 01 Jul 12, 05:07

And how would i call the num of pending, approved, and declined? Do i have to re-write that and supply WHERE? - Kinda new to PHP and still learning. :) - Khaled

This is the result that am after (Total Comments: 4 - Pending Comments: 1 - Approved Comments: 2 - Declined Comments: 1) - Khaled

Thanks a lot, much appreciated. - Khaled

Just out of curiosity .. This code added 4 extra lines to my original code. So, why is this function better than using mysql_num_rows? - Khaled

Generally speaking, the more work you can offload to your database server, the better. Database servers are highly optimized to perform these types of operations. This is why I also suggested that you get all of the results in a single query below; the overhead you're incurring by running a bunch of little queries instead of handing one off to the database server and letting it crunch it will kill any sizable application. mysql_num_rows() returns an entire resultset which you're not using. Depending on how big that resultset is, you could kill your app. COUNT() returns just one single row. - Rey Skippus

Utiliza select count(*) from comments where .....

Respondido 14 Jul 12, 12:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.