Skip to content
Advertisement

what does this hyphen do between columns in order by clause in mysql query?

what does this hyphen do between updated_at and created_at columns in order by clause ?

select * from `users` order by updated_at - created_at DESC 

Advertisement

Answer

It’s a minus sign, so it subtracts the created date from the updated date on each row and uses the result for ordering the rows that are returned.

The are both timestamps, with the created date coming first and thus being a lower number. The greater the difference between the created date and updated date the higher up in the results it will be using a descending order, with results that have the same updated and created date coming last.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement