无法在Laravel 4中运行原始查询 [英] Cannot run raw Query in Laravel 4
本文介绍了无法在Laravel 4中运行原始查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要获取总数才能显示在我的页面中.
I need to get total count to show in my page.
我可以运行此&遍历&得到总数
I can run this & loop through & get the total number
DB::table('table1')
->select((DB::raw('MAX(score)')))
->where('status', 1)
->groupBy('user_id')
->get();
但是此查询仅在一次查询中就可以给我计数&我不需要运行任何额外的循环来获取总数.
But this query would give me the count in just a single query & I don't need run any extra loop to get the total.
SELECT COUNT( * ) FROM (
SELECT MAX( score ) FROM table1
WHERE status =1
GROUP BY user_id
) AS totalCounter
我应该如何在Laravel 4中运行此RAW查询?
How am I suppose to run this RAW query in Laravel 4?
推荐答案
其他贡献者提到;-
DB::select('SQL QUERY GOES HERE WITH PARAMETERS ?, ?', array('parameter 1', 'parameter 2'));
上面的代码应允许使用原始sql.
The code above should permit raw sql.
但是
DB::table('table1')
->select((DB::raw('MAX(score)')))
->where('status','=', 1)
->groupBy('user_id')
->count();
对于您要完成的任务应能达到相同的效果,并且与laravels哲学更加吻合.
Should achieve the same effect for the task you seek it for, and is more in tune with laravels philosophy.
这篇关于无法在Laravel 4中运行原始查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文