php mysql order by timestamp is incorrect

后端 未结 2 1058
说谎
说谎 2021-01-29 12:05

I have database records such as below

Name Date(timestamp) Time(timestamp)

I want to order them by time DESC. But it shows me incorrect order.

相关标签:
2条回答
  • 2021-01-29 12:18

    Are you sure that select only for the date that you want? Because I think that select multiple date not only one.

    Also for what you want you need to use ASC instead of DESC.

    Because I have try and it's work for me. This is my simple code :

    $sql = "SELECT * FROM test WHERE date = '2016-02-10' ORDER BY time ASC";
    $res = $DB->query($sql);
    
    while($enr = $res->fetch()){
        var_dump($enr);
    }
    

    With this DB :

    And the result is :

    array (size=8)
      'id' => string '4' (length=1)
       0 => string '4' (length=1)
      'date' => string '2016-02-10' (length=10)
       1 => string '2016-02-10' (length=10)
      'time' => string '01:00:07' (length=8)
       2 => string '01:00:07' (length=8)
    
    array (size=8)
      'id' => string '3' (length=1)
       0 => string '3' (length=1)
      'date' => string '2016-02-10' (length=10)
       1 => string '2016-02-10' (length=10)
      'time' => string '02:03:05' (length=8)
       2 => string '02:03:05' (length=8)
    

    The result is order in the ASC order like you want

    Hope that helps you.

    0 讨论(0)
  • 2021-01-29 12:45

    Try to use Timestamp() for specifying that you expect the fields you wanna extract data from as timestamps.

    something like this:

    SELECT timestamp( `timestamp` ) as 'timestamp'
    FROM randomTable
    ORDER BY 1 ASC;
    

    it would be great if you can post screenshots of your output to make the issue clearer.

    0 讨论(0)
提交回复
热议问题