Jump to content

Counting Select Distincts


QuePID

Recommended Posts

Hi,

 

I have a database with roughly 73k rows (records).  I am using a query string such as:

SELECT DISTINCT Column FROM databasename

 

I am then using mysql_num_rows($results) to count the number of distinct rows.  This method works, but it is slow.

 

Is there a faster way of doing this perhaps internal to the mysql without having to send the entire results to the PHP when I only need a count?

Link to comment
https://forums.phpfreaks.com/topic/236777-counting-select-distincts/
Share on other sites

select count( distinct column ) from databasename

 

When I echo the results of your query string I get "Resource id #6", and when I use mysql_num_rows($results) I get a value of 1 which isn't correct (should be 224).  What is the type of data returned in the results of the query?

Using fenway's query with PHP code:

<?php
$results = mysql_query("SELECT COUNT(DISTINCT column) AS myCount FROM table");
$values = mysql_fetch_assoc($results);

echo $values['myCount'];
?>

 

Worked like a charm, and was quite fast.  How would I do similar for a count of all records within the table?

Worked like a charm, and was quite fast.  How would I do similar for a count of all records within the table?

 

<?php
$results = mysql_query("SELECT COUNT(*) AS totalCount FROM table");
$values = mysql_fetch_assoc($results);

echo $values['totalCount'];
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.