【发布时间】:2015-06-10 18:10:22
【问题描述】:
我在数据库中有一个DATETIME 类型的列,格式为yyyy-mm-dd hh:mm:ss。尽管如此,作为信息,我只得到了日期(我不在乎时间)。那么我应该如何查询数据库以仅按日期搜索该列,即使该值同时包含日期和时间?
更新:
这是我尝试选择实体的 PHP 代码:
public static function getTripsWithDrivers($route_from = null, $route_to = null, $date = null)
{
$trips = Trip::whereRouteFrom($route_from)->whereRouteTo($route_to)->whereStartDate($date);
...
}
【问题讨论】:
-
最佳解决方案是
BETWEEN date1 AND date2,其中date1与时间00:00:00一致,date2与时间23:59:59一致。这将使用字段上的索引,如果你有的话。函数DATE(date)不会使用索引。
标签: php mysql datetime laravel laravel-4