我是Laravel的初学者。我可以在原始PHP中编写查询。现在我想在Laravel框架中编写它。我已阅读文档并尝试到目前为止:
在原始PHP中:
SELECT count(*)/(TIMESTAMPDIFF(YEAR,'2016-01-01 00:00:00',now())+1) as avg from buyer where created_at >= '2016-01-01 00:00:00';
与Laravel一起尝试:
public function index(){
//
$yrUserReg = DB::table('buyer')
-> SELECT (DB::raw (count('*')/("TIMESTAMPDIFF(YEAR,'2016-01-01 00:00:00',now())+1")))
-> AS ('avg')
-> WHERE ("created_at >= '2016-01-01 00:00:00'");
return view('userflow',compact($yrUserReg));
}
但它给我一个错误: Division by Zero
。我找不到任何解决方法。
提前致谢。任何帮助将不胜感激!!
答案 0 :(得分:0)
以下是完整构建的查询:
$yrUserReg = DB::table('buyer')
->select(DB::raw("(count('*')/(TIMESTAMPDIFF(YEAR,'2016-01-01 00:00:00',now())+1)) as avg"))
->where('created_at', '>=', '2016-01-01 00:00:00')
->get();