我有一个用户访问表,我想做一个查询,该查询按日期返回我的访问次数。
我的表具有以下列:
id
type -> int
id_user -> int
created_at -> datetime
我正在尝试此查询:
$admin = userAccess::distinct()
->select(DB::raw('count(*) as total ') , DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d")'), DB::raw('DAYNAME(created_at) as dia'))
->where('created_at', '>=', DB::raw('DATE(NOW()) - INTERVAL 7 DAY'))
->where('type', 1)
->groupBy('created_at')
->limit(7)
->get();
通过此查询,它返回我:
Monday(2018-10-08) -> 1
Tuesday(2018-10-09) -> 1
我想要的结果是:
Monday(2018-10-08) -> 3
Tuesday(2018-10-09) -> 1
我做错了什么?
谢谢
答案 0 :(得分:0)
尝试以下操作 您必须按日期分组,例如:'2018-10-08'而不是时间戳
$admin = userAccess::distinct()
->select(DB::raw('count(*) as total ') , DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d") as created_date '), DB::raw('DAYNAME(created_at) as dia'))
->where('created_at', '>=', DB::raw('DATE(NOW()) - INTERVAL 7 DAY'))
->where('type', 1)
->groupBy('created_date')
->limit(7)
->get();