Log in

View Full Version : Mysql Order by



Lemon
04-10-2010, 04:53 AM
Hey,

In mysql can you order items by when they were last updated?
like


$last_updated = mysql_query("SELECT id,name,email,status FROM user WHERE status = 'online' ORDER BY ");

What would I put order by to get the last updated ones first?

Thanks

james438
04-10-2010, 05:06 AM
I'm pretty sure that you can't. You need to have a column that states when your entry was last updated and have that field updated every time you update that particular entry.


$last_updated = mysql_query("SELECT id,name,email,status FROM
user WHERE status = 'online' ORDER BY date2 desc");

or something like that will work.

Lemon
04-10-2010, 05:45 AM
I'm pretty sure that you can't. You need to have a column that states when your entry was last updated and have that field updated every time you update that particular entry.


$last_updated = mysql_query("SELECT id,name,email,status FROM
user WHERE status = 'online' ORDER BY date2 desc");

or something like that will work.

Yeh, I have a last edited field but it is day month year, I want it to be more precise like with minutes aswell otherwise it just orders the day in alphabetical order...

djr33
04-10-2010, 05:51 AM
Correct. So change how it stores the information. For reasons like this (just that generally it is more flexible) it's very common to use a timestamp instead of any formatted date. From that you can get a formatted date or use the timestamp as-is, such as for ordering.