My SQL Query does not work correctly?

-1

I have a system that uses dates for logging and I separate the date by an arrays-formed explode and register them in a database, but when I do the query it looks for them in order, the order stumbles.

Following code examples - >

      <?php 
                    $listar = new USER();
     $stmt = $listar->runQuery("SELECT * FROM horario ORDER BY dia,mes,ano DESC LIMIT 5");
    $stmt->execute();
    $Row=$stmt->fetchAll(PDO::FETCH_ASSOC);

Recent Dates: 11/8/2018

As you can see, you're not picking up the records as you should, please, I need your help!

    
asked by anonymous 14.11.2018 / 02:49

1 answer

1

You are doing an increasing ordering by day and month, and decreasing for just the year.

Your data does not have a timestamp, so mysql knows which is the most recent or oldest date, so I think you can solve this problem with the ID by picking it up by the most recent id.

I think a select like this would solve your case:

 SELECT * FROM horario ORDER BY ano DESC, mes DESC, dia DESC, idh DESC  LIMIT 5
    
14.11.2018 / 03:03