Skip to content
Advertisement

Count data in a table column

I Have a database with several rows called tb_reg, with 4 columns: id, comment, date and id_name. . How can i count the number of rows for the diferent id_name in the id_name column ?

I would like to have a return in variables, like that:

$id_name_1 = (number of rows);
$id_name_2 = (number of rows);
$id_name_3 = (number of rows);

I would like to use the result for each id_name in a graph, showing the number of comments per id_user.

Advertisement

Answer

If I understood this right, GROUP BY should do the trick for you.

SELECT id_name, COUNT(id_name) FROM tb_reg GROUP BY id_name;

It will return a list with 2 columns, first is the id_name and second is the count of that name in the whole table. You only have to run this query once, and for storing each result you can just use an array.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement