我在查询时遇到问题,我希望返回包含字段中特定单词且在特定日期范围之间的结果。我现在使用的查询是:
$month_no = date('m');
$month_day = date('d');
$query="SELECT * FROM `table` WHERE ((user_purchased LIKE '%john%') AND (request_date BETWEEN '2011-$month_no-$month_day' AND '2011-$month_no-$month_day')) ";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result); echo $num_rows;
'request_date字段中的日期格式如下: 2011-10-03 12:30:34
关于我做错了什么想法?
谢谢!
答案 0 :(得分:1)
如果你尝试会发生什么:
BETWEEN '2011-$month_no-$month_day 00:00:00' AND
'2011-$month_no-$month_day 23:59:59')) ";
答案 1 :(得分:0)
鉴于您的BETWEEN日期范围相同,为什么不尝试
... WHERE (DATE(requested_date) = "2011-$month_no-$month_day")
答案 2 :(得分:0)
狂野的猜测,因为我只知道如何使用Oracle数据库 - 你可以使用* to_date *函数,如
... WHERE date = TO_DATE('20110820','YYYYMMDD') ...
或
... WHERE (date => TO_DATE('20110801','YYYYMMDD')) AND (date <= TO_DATE('20110831','YYYYMMDD'))
也许值得检查mysql是否还需要这样的函数调用。
答案 3 :(得分:0)
试
$m = date('m');
$d = date('d');
$qry = " SELECT * FROM `table`
WHERE ( requested_date BETWEEN DATE_FORMAT('2011-$m-$d', ''%Y-%m-%d'')
AND DATE_FORMAT('2011-$m-$d', ''%Y-%m-%d'') )
AND user_purchased LIKE '%john%' ";
答案 4 :(得分:0)
如果您想检查request_date
(我认为是日期时间)是否有日期部分'2011-$month_no-$month_day'
,请尝试以下操作:
$query="SELECT * FROM `table`
WHERE (user_purchased LIKE '%john%')
AND (request_date >= '2011-$month_no-$month_day')
AND (request_date < '2011-$month_no-$month_day' + INTERVAL 1 DAY)
";