Query to sort by two Mysql dates

Asked

Viewed 162 times

4

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

The aim is for the listing to be ordered as follows:

  • if the data_atualizada_noticia is different from 0000-00-00 and greater than data_criacao_noticia should appear first, or the latest date always appears first.

In the next image is an example of what I’m trying: inserir a descrição da imagem aqui

I’m using php and mysql to query. It’s possible to use ternary operators in the query or something like that?

  • Can sort by code or needs to be in the query?

  • It has to be in the query if possible @Danielgregatto

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
  • That’s right, thank you @Danielgregatto

Browser other questions tagged

You are not signed in. Login or sign up in order to post.