views:

839

answers:

2

There are 2 columns that I want data from. Basically I want to see new signups per day. Go easy on me, my first day with mysql (or any DB for that matter)

So far I have this

mysql> select created, count(id) from user group by created;
+---------------------+-----------+
| created             | count(id) |
+---------------------+-----------+
| 2009-05-27 00:58:56 |         1 | 
| 2009-05-27 01:27:30 |         1 | 
| 2009-05-27 02:35:37 |         1 | 
| 2009-05-27 02:58:56 |         1 | 
| 2009-05-27 03:36:03 |         1 | 
| 2009-05-27 03:38:03 |         1 | 
| 2009-05-27 03:48:16 |         1 | 
| 2009-05-27 03:51:37 |         1 |

The output I would like is

+---------------------+----------------------+
| created             | user                 |
+---------------------+----------------------+
| 2009-05-27          | 10                   |
| 2009-05-28          | 13                   | 
| 2009-05-29          | 19                   |

Any help is appreciated!

Thanks

+4  A: 
select DATE(created), count(id) from user group by DATE(created);
Todd Gardner
+1 for quick and correct answer. You could also add column aliases to make it easier to access the result from the query in the language of choice.
PatrikAkerstrand
+1  A: 
SELECT DATE(created) as date, count(1) as nrRegistered
FROM user
GROUP BY date

Note: By counting 1 instead of id we avoid a lookup in the table and enable an index-only query

PatrikAkerstrand
That's assuming the created field is indexed.
Todd Gardner
Yes it is. It also assumes that MySQL isn't clever enough to use the id (which is most probably a primary key) that is appended to the index when doing count(id). So I have to admit that it's a pretty loose note. But I didn't say that my query forces an index-only join, just that it makes it possible. I'd suggest using an EXPLAIN [...] on the query to make sure.
PatrikAkerstrand
clarification: The primary key is appended if the storage engine is InnoDB, not MyISAM.
PatrikAkerstrand