在 Laravel where 子句中等效使用 mysql datediff 函数



我对mysql有以下查询。

phpMyAdmin 4.0.10.10

SELECT * FROM `table_name` WHERE DATEDIFF(NOW(), start_date) = 154

我需要对 Laravel 进行等效查询才能在 where 子句中使用 DATEDIFF 函数。

正如您的评论,您需要什么 laravel,

$oldDate = Carbon::now()->subDays(154));
Table::whereDate('start_date', '=', $oldDate); 

另一种方法是:

DB::table('table_name')->whereRaw('DATEDIFF(current_date, start_date) = 154')->get();

你可以尝试这样的东西:

SELECT * FROM `table_name` WHERE start_date = (NOW() - 154 days);

我不会在 WHERE 中推荐 DATEDIFF,因为它可能会强加表格扫描。相反,我建议这样做:

SELECT
DATEDIFF(start_date, NOW()) AS daysDiffs
FROM table_name
WHERE start_date = DATE(NOW() - INTERVAL 154 DAYS);

最新更新