我正在编写一个查询,它应该只返回当前一天的数据,并且希望使用结束时间和开始时间。
就像这样。
$endtime = date ("Y-m-d H:i:s");
$starttime = date ("Y-m-d H:i:s") - 1; // Not working..
$sql = "select * from tbl_order where transdate between $starttime and $endtime";你能帮忙吗?
RJ
发布于 2014-03-03 02:26:56
不能百分之百确定你要达到什么目的,但是如果你想从过去的24小时(注意:不是最后一天)得到订单,这就可以了:
$endtime = date ("Y-m-d H:i:s");
$starttime = date ("Y-m-d H:i:s", (time() - (60*60*24)));尽管我要说,用纯SQL来完成这个任务更合适。
发布于 2014-03-03 02:34:41
between关键字操作日期值,而不是字符串。
<?php
$jan = '2014-01';
$feb = '2014-02';
$sql = "select record_ts from table where date(record_ts)
between str_to_date('$jan', '%Y-%m')
and str_to_date('$feb', '%Y-%m');
";
?>注意,我必须将record_ts转换为(MySQL)日期,因为列被定义为时间戳,所以还必须转换日期字符串。
。。。
卡尔·马斯卡姆,你的评论可能对MySQL的最新版本有效。
mysql> select record_ts from table where date(record_ts)
between '2014-01'
and '2014-02' ;
Empty set, 2 warnings (0.01 sec)
mysql> select record_ts from table where date(record_ts)
between str_to_date('2014-01', '%Y-%m')
and str_to_date('2014-02', '%Y-%m');
(...)
1119 rows in set (0.01 sec)
$ mysql --version
mysql Ver 14.14 Distrib 5.1.70, for pc-linux-gnu (x86_64) using readline 5.1https://stackoverflow.com/questions/22137561
复制相似问题