I have a table which contains many records. I’m looking for an efficient way to count how many times an element is repeated.
Example table:
id name
1 ana
2 john
3 tom
4 ana
5 john
I’ve done so far , but it is inefficient.
$number = 0;
for ($i =1 ; $i <= mysql_num_rows($query) ; $i++)
for ($j = 1 ; $j <=mysql_num_rows($query); $j++)
if ( $table[$i] == $table[$j])
$number++;
if ($number > 2 )
echo $number;
You can do this directly in SQL
It will be more efficient, since you don’t have to transfer every row to PHP.