View Single Post
Old 04-30-2009, 05:18 PM  
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
This should help you out

mysql> desc test;
+-------+--------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+-------------------+-------+
| title | varchar(100) | YES | | NULL | |
| views | int(11) | YES | | NULL | |
| time | timestamp | YES | | CURRENT_TIMESTAMP | |
+-------+--------------+------+-----+-------------------+-------+

mysql> select * from test;
+---------------------------------+-------+---------------------+
| title | views | time |
+---------------------------------+-------+---------------------+
| news headline | 3300 | 2009-04-30 15:22:57 |
| another news headline | 2300 | 2009-04-30 15:23:13 |
| another news headline yet again | 5430 | 2009-04-30 15:23:27 |
| another news headline yet again | 1060 | 2009-04-29 15:23:48 |
| another news headline | 6630 | 2009-04-29 15:24:10 |
| news headline | 3200 | 2009-04-29 15:24:25 |
+---------------------------------+-------+---------------------+
6 rows in set (0.00 sec)

mysql> select date(time) as date, views, title from test group by time order by date desc, views desc;
+------------+-------+---------------------------------+
| date | views | title |
+------------+-------+---------------------------------+
| 2009-04-30 | 5430 | another news headline yet again |
| 2009-04-30 | 3300 | news headline |
| 2009-04-30 | 2300 | another news headline |
| 2009-04-29 | 6630 | another news headline |
| 2009-04-29 | 3200 | news headline |
| 2009-04-29 | 1060 | another news headline yet again |
+------------+-------+---------------------------------+
6 rows in set (0.00 sec)

That last one there is what you want.

Keep in mind you obviously want to index your fields, and if there are two records with identical timestamps, you either want to group by time,title so they don't get grouped as one or add a primary key (like an auto increment id) to group by time,ID (preventing the rare case you have an entry with exactly the same title and time heh).

Enjoy
__________________
Skype variuscr - Email varius AT gmail
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote