Php MySQL汇总到每年的特定日期
我有一个Php MySQL汇总到每年的特定日期,php,mysql,laravel,Php,Mysql,Laravel,我有一个customer\u invoices表,其中包含一个字段invoited\u atas date&total\u price,我需要从每年年初到今天的同一天发票的总价之和,按年份分组,从我提供的特定年份开始 到目前为止我所做的: public function totalIncomeToCurrentDayByYear($startingAt) { return CustomerInvoice::selectRaw('sum(total_price) as total')
customer\u invoices
表,其中包含一个字段invoited\u at
as date&total\u price
,我需要从每年年初到今天的同一天发票的总价之和,按年份分组,从我提供的特定年份开始
到目前为止我所做的:
public function totalIncomeToCurrentDayByYear($startingAt)
{
return CustomerInvoice::selectRaw('sum(total_price) as total')
->where('customer_invoices.status', Status::VALIDATED)
->selectRaw("DATE_FORMAT(invoiced_at,'%Y') as year")
->whereRaw("DATE_FORMAT(invoiced_at,'%Y') >= $startingAt")
->groupBy('year')
->get()
->pluck('total', 'year');
}
我需要有一个类似的结果:(在这种情况下,我得到的是全年的总和,这不是我想要的):
例如,今天是1月23日
,对于数组中的每一行,应表示该年截至1月23日
的总价
之和在其中放置另一个过滤器,以使发票日期小于当前日期
public function totalIncomeToCurrentDayByYear($startingAt,$currentDate) { return CustomerInvoice::selectRaw('sum(total_price) as total') ->where('customer_invoices.status', Status::VALIDATED) ->selectRaw("DATE_FORMAT(invoiced_at,'%Y') as year") ->whereRaw("DATE_FORMAT(invoiced_at,'%Y') >= $startingAt")->whereRaw("invoiced_at <= $currentDate") ->groupBy('year') ->get() ->pluck('total', 'year'); }
public function totalIncomeToCurrentDayByYear($startingAt,$currentDate){return CustomerInvoice::selectRaw('sum(总价)as total')->where('customer_invoices.status',status::VALIDATED)->selectRaw('DATE_格式(invoices_格式,'%Y')>=$startingAt)->whereRaw(“在处开具发票,请尝试下面的查询
CustomerInvoice::whereRaw("DATE_FORMAT(invoiced_at,'%Y') >= $startingAt")
->where('customer_invoices.status', Status::VALIDATED)
->whereRaw('MONTH(invoiced_at) < MONTH(NOW()) or
(MONTH(invoiced_at) = MONTH(NOW()) and DAY(invoiced_at) <=
DAT(NOW()))')
->select(DB::raw('YEAR(invoiced_at) invoiced_year' ),
DB::raw('SUM(total_price) total_price'))-
>groupBY(DB::raw('YEAR(invoiced_at)')
->orderBy(DB::raw('YEAR(invoiced_at)')->get();
CustomerInvoice::whereRaw(“日期格式(发票日期为,'%Y')>=$startingAt”)
->其中('customer\u invoices.status',status::VALIDATED)
->其中('月份(发票日期)<月份(现在())或
(月(开票日期)=月(现在())和日(开票日期)直到我提到的今天,这还没有得到我每年的总数。@dwix我添加了另一个认为可以解决您的问题的地方,请检查是的,谢谢,这是有效的。您能花点时间向我解释一下您添加的地方的实际用途吗?欢迎,如果有帮助,请选择我的答案就是解决方案。”->whereRaw('MONTH(invoited_at)CustomerInvoice::whereRaw("DATE_FORMAT(invoiced_at,'%Y') >= $startingAt")
->where('customer_invoices.status', Status::VALIDATED)
->whereRaw('MONTH(invoiced_at) < MONTH(NOW()) or
(MONTH(invoiced_at) = MONTH(NOW()) and DAY(invoiced_at) <=
DAT(NOW()))')
->select(DB::raw('YEAR(invoiced_at) invoiced_year' ),
DB::raw('SUM(total_price) total_price'))-
>groupBY(DB::raw('YEAR(invoiced_at)')
->orderBy(DB::raw('YEAR(invoiced_at)')->get();