繁体   English   中英

Laravel SQL选择查询,其中DateTimestamp包含此月份和年份

[英]Laravel SQL select query where DateTimestamp contains this month and year

我正在尝试进行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.*');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM