Page 1 of 1

Counting Distinct Rows in MySQL

Posted: Mon Apr 28, 2003 10:55 pm
by fatal
im trying to count the distinct rows in one of my tabels. Ive tired...

Code: Select all

$sql = "SELECT COUNT(DISTINCT) FROM hits";
$distinct = mysql_query( $sql );

echo "Unique Hits: " . $distinct ;
and....

Code: Select all

$sql = "SELECT DISTINCT FROM hits";
$distinct = mysql_num_rows( mysql_query( $sql ) );

echo "Unique Hits: " . $distinct ;
Niether worked... anyone have any idea why?

Thanks!

Posted: Mon Apr 28, 2003 11:13 pm
by fractalvibes
You need to specify Distict column_name.

If I have a table such as:
ID
FirstName
LastName
Comments


Then Select Distinct Lastname From sometable
or
Select Distinct FirstName From sometable

So you need to tell the db what distinct values you want.

Phil J.

Posted: Mon Apr 28, 2003 11:26 pm
by fatal
Good call fractalvibes, worked great!

Posted: Mon Apr 28, 2003 11:49 pm
by fractalvibes
Good to hear! Glad you got it worked out ok.

Phil J.