Counting Distinct Rows in MySQL

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
fatal
Forum Contributor
Posts: 118
Joined: Sat Apr 20, 2002 10:47 am
Location: East Coast

Counting Distinct Rows in MySQL

Post 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!
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post 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.
User avatar
fatal
Forum Contributor
Posts: 118
Joined: Sat Apr 20, 2002 10:47 am
Location: East Coast

Post by fatal »

Good call fractalvibes, worked great!
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

Good to hear! Glad you got it worked out ok.

Phil J.
Post Reply