php mysql按时间戳排序不正确 [英] php mysql order by timestamp is incorrect
本文介绍了php mysql按时间戳排序不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有如下数据库记录
Name Date(timestamp) Time(timestamp)
我想按时间DESC订购它们.但这告诉我顺序不正确.现在显示:
I want to order them by time DESC. But it shows me incorrect order. It shows now:
Name 19:00
Other 18:30
One more 19:00
如您所见,时间从最早到最近都没有.我希望它像这样:
As you can see hours are not going from earliest to latest. I want it to be like this:
Other 18:30
Name 19:00
One more 19:00
我在做什么错?
SELECT * FROM table WHERE date='".$date."' ORDER BY book_time DESC
推荐答案
确定要仅选择所需的日期吗?因为我认为选择多个日期不仅是一个.
Are you sure that select only for the date that you want? Because I think that select multiple date not only one.
对于您想要的内容,还需要使用ASC而不是DESC.
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);
}
使用此数据库:
结果是:
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)
结果是按您希望的ASC顺序排列
The result is order in the ASC order like you want
希望对您有帮助.
这篇关于php mysql按时间戳排序不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文