无法在 Laravel 5.1 中获取每周数据
Unable to get data on weekly basis in Laravel 5.1
我想每周从 table 获取数据,就像我想获取周一、周二等注册的记录总数。我可以在 MySQL 中创建一个脚本并且它工作正常,但是当我尝试在 Laravel 中做同样的事情时它会抛出一个错误:
MySQL 查询(工作正常):
SELECT HOUR(`created_on`) AS Hour, COUNT(*) AS calls
FROM `buy_credits_requests`
GROUP BY HOUR(`created_on`)
Laravel 查询(无效)
$credits = \DB::table('buy_credits_requests')->where('status', '=', 1)->groupBy('Hour', 'ASC')
->get(['HOUR(created_on) AS Hour', 'COUNT(*) AS calls', 'state']);
截图如下:
.
试试这个,
$credits = \DB::table('buy_credits_requests')->select(DB::raw('HOUR(created_on) AS Hour'),DB::raw('COUNT(*) AS calls'))->where('status', 1)->groupBy('Hour')
->get();
你的语法有问题,
希望这有帮助。
我想每周从 table 获取数据,就像我想获取周一、周二等注册的记录总数。我可以在 MySQL 中创建一个脚本并且它工作正常,但是当我尝试在 Laravel 中做同样的事情时它会抛出一个错误:
MySQL 查询(工作正常):
SELECT HOUR(`created_on`) AS Hour, COUNT(*) AS calls
FROM `buy_credits_requests`
GROUP BY HOUR(`created_on`)
Laravel 查询(无效)
$credits = \DB::table('buy_credits_requests')->where('status', '=', 1)->groupBy('Hour', 'ASC')
->get(['HOUR(created_on) AS Hour', 'COUNT(*) AS calls', 'state']);
截图如下:
试试这个,
$credits = \DB::table('buy_credits_requests')->select(DB::raw('HOUR(created_on) AS Hour'),DB::raw('COUNT(*) AS calls'))->where('status', 1)->groupBy('Hour')
->get();
你的语法有问题,
希望这有帮助。