我的数据库中有一个表scans,其中记录了用户的扫描活动,并且用户一天可以进行多个活动。我正在寻找获取特定用户在几天内所有扫描的总和的最佳方法。
我已经在代码中尝试了以下操作,但我想我遗漏了一些东西,因为我想聚合当天进行的用户的扫描。
DB::table('scans')->select(DB::raw('count(*) as total, scanner, created_at'))->where('scanner', 'Jon Snow')->groupBy('created_at')->get()
=> Illuminate\Support\Collection {#3313
all: [
{#3304
+"total": 1,
+"scanner": "Jon Snow",
+"created_at": "2020-07-26 19:43:58",
},
{#3321
+"total": 1,
+"scanner": "Jon Snow",
+"created_at": "2020-07-26 19:32:40",
},
{#3327
+"total": 1,
+"scanner": "Jon Snow",
+"created_at": "2020-07-28 19:44:18",
},
{#3323
+"total": 1,
+"scanner": "Jon Snow",
+"created_at": "2020-07-28 20:30:30",
},
所以代替这个:
{#3304
+"total": 1,
+"scanner": "Jon Snow",
+"created_at": "2020-07-26 19:32:58",
},
{#3321
+"total": 1,
+"scanner": "Jon Snow",
+"created_at": "2020-07-26 19:43:40",
},
我想要这个:
{#3304
+"total": 2,
+"scanner": "Jon Snow",
+"created_at": "2020-07-26",
},
{#3304
+"total": 5,
+"scanner": "Jon Snow",
+"created_at": "2020-07-28",
},
白猪掌柜的