views:

753

answers:

3

I am trying to find a record in the database that has the lowest value of its datetime column. In simpler words, I want to find a record with the earliest time.

I can use minimum to find the lowest value, but that will only return the value itself, and not the whole record.

I suppose I could create another query, but I'm wondering if there is a more efficient way of doing this.

A: 

I'm not sure about RoR, but with SQL you can just sort the results of your query by the datetime field and limit the number of results to 1.

SQL:

SELECT field1,field2,... FROM table ORDER BY datefield LIMIT 1;

Or maybe in Ruby (not sure though):

table.find(:all, :limit => 1, :order => 'datefield.asc')
Jordi
+2  A: 
Table.first(:order => 'created_on')
Ken
+11  A: 
John Topley