views:

35

answers:

3

(Sorry for the title, I don't really know how to phrase that :-) )

I have a table that has a date and a uid fields.

I need to get the number of uid for each date, currently I'm doing it in php running multiple queries like this one:

SELECT COUNT(uid) FROM users where Date = 'xxx';

Is there a simple way to achieve this with only an sql query?

+3  A: 

Use the group by clause:

SELECT Date, COUNT(uid) FROM users group by Date;
Guillaume
@0plus1: Note that this doesn't count unique values, it counts **all** values. If this is what you wanted you should probably adjust the title of the question to match.
Mark Byers
A: 
SELECT Date, COUNT(uid) FROM users GROUP BY Date
Theo
+1  A: 

To count the number of unique values use DISTINCT:

SELECT COUNT(DISTINCT uid) AS cnt
FROM users
GROUP BY `Date`

If your column is a datetime then you should use the DATE function to get the date part only:

SELECT COUNT(DISTINCT uid) AS cnt
FROM users
GROUP BY DATE(`Date`)
Mark Byers