Laravel-将多个查询合并为一个,还包括GroupBy

时间:2019-07-02 08:28:18

标签: laravel eloquent

我写了不同的查询,这些查询针对不同的事物起作用。我想将它们合并为一个(销售,今天,当前周,当前周,当前月,当前年和总体销售额),并按一个称为商店的字段分组。

            $currentYear = date('y');
        $currentyearbilling = DB::table("billings")
               ->select(DB::raw("SUM(amount) as total"))
               ->whereRaw('YEAR(created_at) = ?',$currentYear)
               ->get();

        $currentMonth = date('m');
        $currentmonthbilling = DB::table("billings")
               ->select(DB::raw("SUM(amount) as total"))
               ->whereRaw('MONTH(created_at) = ?',$currentMonth)
               ->get();

        $currentWeek = date('w');
        $currentweekbilling = DB::table("billings")
               ->select(DB::raw("SUM(amount) as total"))
               ->whereRaw('WEEK(created_at) = ?',$currentWeek)
               ->get();

        $currentDay = date('d');
        $currentdaybilling = DB::table("billings")
               ->select(DB::raw("SUM(amount) as total"))
               ->whereRaw('(created_at) = ?',$currentDay)
               ->get(); 
  

按商店分组。

表名称称为计费。我仍然要遵循这种格式

  

DB :: table(“ billings”)   我该如何实现?

syntax error

错误:

2 个答案:

答案 0 :(得分:0)

您应该尝试以下操作:

$currentYear = date('y');
$currentMonth = date('m');
$currentWeek = date('w');
$currentDay = date('d');

$currentyearbilling = DB::table("billings")
       ->select(DB::raw("SUM(amount) as total"))
       ->whereRaw('YEAR(created_at) = ?',$currentYear)
       ->orWhereRaw('MONTH(created_at) = ?',$currentMonth)
       ->orWhereRaw('WEEK(created_at) = ?',$currentWeek)
       ->orWhereRaw('(created_at) = ?',$currentDay)
       ->groupBy('store')
       ->get();

答案 1 :(得分:0)

我们说我们有一个像这样的表billings

| column     | type       |
|------------|------------|
| id         | int        |
| store      | varchar    |
| amount     | int        |
| created_at | timestamps |
| updated_at | timestamps |

通过使用以下子查询,我将获得current_daycurrent_monthcurrent_weekcurrent_yearoverall中每个商店的数量:

select 
  store,
  (SELECT SUM(amount) from billings as child where child.store = parent.store and DAY(created_at) = ? and MONTH(created_at) = ? and YEAR(created_at) = ?) as current_day,
  (SELECT SUM(amount) from billings as child where child.store = parent.store and WEEK(created_at) = ?) as current_week,
  (SELECT SUM(amount) from billings as child where child.store = parent.store and MONTH(created_at) = ? and YEAR(created_at) = ?) as current_month,
  (SELECT SUM(amount) from billings as child where child.store = parent.store and YEAR(created_at) = ?) as current_year,
  SUM(amount) as overall,
  COUNT(id) as total_rows

from billings as parent

group by store

使用Eloquent,将是这样的:

$childQuery = \DB::table(\DB::raw('billings as child'))
    ->select(\DB::raw('SUM(amount) as amount'))
    ->whereRaw('child.store = parent.store');

$currentDayQuery = (clone $childQuery)->whereDay('created_at', now()->day)->whereMonth('created_at', now()->month)->whereYear('created_at', now()->year);
$currentWeekQuery = (clone $childQuery)->where(\DB::raw('WEEK(created_at)'), now()->weekOfYear);
$currentMonthQuery = (clone $childQuery)->whereMonth('created_at', now()->month);
$currentYearQuery = (clone $childQuery)->whereYear('created_at', now()->year);

$rows = \DB::table(\DB::raw('billings as parent'))
    ->select([
        'store',
        \DB::raw('(' . $currentDayQuery->toSql() . ') as current_day'),
        \DB::raw('(' . $currentWeekQuery->toSql() . ') as current_week'),
        \DB::raw('(' . $currentMonthQuery->toSql() . ') as current_month'),
        \DB::raw('(' . $currentYearQuery->toSql() . ') as current_year'),
        \DB::raw('SUM(amount) as overall'),
        \DB::raw('COUNT(id) as total_rows')
    ])
    ->mergeBindings($currentDayQuery)
    ->mergeBindings($currentWeekQuery)
    ->mergeBindings($currentMonthQuery)
    ->mergeBindings($currentYearQuery)
    ->groupBy('store')
    ->get();

希望这会有所帮助。