【问题标题】:Mysql get records with specific dateMysql获取特定日期的记录
【发布时间】:2015-01-11 11:23:47
【问题描述】:

我有一个表,其中包含一个名为日期的字段,类型为时间戳。我只会检索特定日期的行。

我的 PHP 脚本以 dd-MM-YYYY 格式接收日期

这就是我解析查询日期的方式:

$converted_date = date("Y-m-d", strtotime($date));

这是我的 WHERE 子句:

WHERE DATE(date) = '" . $converted_date . "' ORDER BY date DESC"

我总是得到一个空的结果。

【问题讨论】:

  • 首先——date是mysql的保留字,至少你应该使用反引号
  • @u_mulder(和upvoter)日期不是保留字

标签: php mysql sql date


【解决方案1】:

如上所述here

永远不要使用像 DATE(datecolumns) = '2012-12-24' 这样的选择器 - 它是性能杀手:

它将计算所有行的 DATE(),包括那些不匹配的行

使用起来更快

SELECT * FROM tablename 
WHERE columname BETWEEN '2012-12-25 00:00:00' AND '2012-12-25 23:59:59'

您需要调整 where 子句以返回当天午夜和午夜之间的所有记录。

$converted_date = date("Y-m-d", strtotime($date));
//$converted_date2 would equal that day up to 23:59:59
WHERE date BETWEEN '" . $converted_date . "' AND '.$converted_date2.' ORDER BY date DESC"

虽然你也可以使用

WHERE date >= CURDATE()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 2014-03-10
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多