Select to sort by hour

Asked

Viewed 852 times

0

Guys, I need to make a select that organizes by date and time. By date it is already working, but if I put an "and" "hour" after the date, it messes up all the records. See my select:

$dados = Connection::select("Select *,id, data, mes,date_format(`data`,'%d/%m/%Y') as `data_formatada` FROM (SELECT Day(data) AS dia, year(data) AS ano,    
                    (CASE month(data) 
                       when 1 then 'Janeiro'
                       when 2 then 'Fevereiro'
                       when 3 then 'Março'
                       when 4 then 'Abril'
                       when 5 then 'Maio'
                       when 6 then 'Junho'
                       when 7 then 'Julho'
                       when 8 then 'Agosto'
                       when 9 then 'Setembro'
                       when 10 then 'Outubro'
                       when 11 then 'Novembro'
                       when 12 then 'Dezembro'
                       END) AS mes,
                    (CASE WEEKDAY(data) 
                       when 0 then 'Segunda-feira'
                       when 1 then 'Terça-feira'
                       when 2 then 'Quarta-feira'
                       when 3 then 'Quinta-feira'
                       when 4 then 'Sexta-feira'
                       when 5 then 'Sábado'
                       when 6 then 'Domingo'                 
                       END) AS DiaDaSemana,
                       id, 
                       data,hora,compromisso,pessoa,local FROM agenda WHERE not (data is null)) as agenda order by data asc"

1 answer

3


The ORDER BY command accepts multiple parameters:

ORDER BY campo1 ASC, campo2 ASC

Then your appointment would be:

$dados = Connection::select("
        Select 
            *,
            date_format(`data`,'%d/%m/%Y') as `data_formatada` 
        FROM (
            SELECT 
                Day(data) AS dia, 
                year(data) AS ano,
                (
                    CASE month(data) 
                       when 1 then 'Janeiro'
                       when 2 then 'Fevereiro'
                       when 3 then 'Março'
                       when 4 then 'Abril'
                       when 5 then 'Maio'
                       when 6 then 'Junho'
                       when 7 then 'Julho'
                       when 8 then 'Agosto'
                       when 9 then 'Setembro'
                       when 10 then 'Outubro'
                       when 11 then 'Novembro'
                       when 12 then 'Dezembro'
                   END) AS mes,
                (
                    CASE WEEKDAY(data) 
                       when 0 then 'Segunda-feira'
                       when 1 then 'Terça-feira'
                       when 2 then 'Quarta-feira'
                       when 3 then 'Quinta-feira'
                       when 4 then 'Sexta-feira'
                       when 5 then 'Sábado'
                       when 6 then 'Domingo'                 
                       END
                    ) AS DiaDaSemana,
                id, 
                data,
                hora,
                compromisso,
                pessoa,
                local 
            FROM 
                agenda
            WHERE 
                not (data is null)
        ) as agenda

        ORDER BY
            data ASC, 
            hora ASC");
  • Um, I was putting AND. It worked. Thank you.

  • @Eduehi, Your query was a little disorganized, maybe that’s why you didn’t find the error, because it was really something very simple. If you need to organize SQL queries effectively, there are online tools that may be doing this for you [http://www.sql-format.com], [http://sqlformat.org], this may be helping you in the future.

Browser other questions tagged

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