Query to sort by two dates Mysql

4

I have my table tbl_noticias with two dates of type date : data_criacao_noticia and data_atualizada_noticia .

The purpose is for the listing to be sorted as follows:

  • If data_atualizada_noticia is different from 0000-00-00 and greater than data_criacao_noticia should appear first, that is, the most recent date always appears first.

In the following image is the example of what I am trying:

I'm using php and mysql to make the query. Is it possible to use ternary operators in the query or something of the genre?

    
asked by anonymous 26.01.2015 / 19:00

1 answer

3

Try this SQL

SELECT n.*,
(IF (n.data_atualizacao > n.data_criacao, n.data_atualizacao , n.data_criacao)) as data_maior
 FROM tbl_noticias n
 ORDER BY data_maior DESC
    
26.01.2015 / 19:30