我正在尝试进行 SQL 查询,我可以在其中检查 DateTimestamp 是否包含本月和本年。
部分查询:
$leads = DB::table('leads')
->leftJoin('invoices', function($join2) {
$join2->on('leads.id', '=', 'invoices.lead_id');
})
->where('invoices.chance', '!=', 0)
->where('invoices.updated_at', '=', date('m-Y')) //Check if DateTimestamp(updated_at) contains Year and month
->select('leads.*')
我用 date_parse, new DateTime() 试过了,但是没用。我做错了什么?
最佳答案
您可以使用 whereMonth()
和 whereYear()
来实现这一点:
$leads = DB::table('leads')
->leftJoin('invoices', function($join2) {
$join2->on('leads.id', '=', 'invoices.lead_id');
})
->where('invoices.chance', '!=', 0)
->whereMonth('invoices.updated_at', '=', date('m')) // changed
->whereYear('invoices.updated_at', '=', date('Y')) // changed
->select('leads.*');
关于php - DateTimestamp 包含本月和年的 Laravel SQL 选择查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34204617/