Laravel集团按日期

时间:2014-10-28 06:01:27

标签: mysql laravel-4 eloquent

我正在尝试对以下sql进行elqouent查询,

select sum(w.total_item_count) as Day_count, w.created_at as created_at from `sales_flat_orders` as `w` group by DATE(`w`.`created_at`) order by `w`.`created_at` asc

我试过这个

$orderbydate = DB::table('sales_flat_orders as w')
                ->select(array(DB::Raw('sum(w.total_item_count) as Day_count'),DB::Raw('DATE(w.created_at) as created_at')))
                ->groupBy('w.created_at')
                ->orderBy('w.created_at')
                ->get();

我在sql查询中获得了正确的输出,但没有在eloquent查询中, 请帮忙,谢谢。

2 个答案:

答案 0 :(得分:7)

所以伙计们,我已经得到了答案,谢谢你阅读我的问题。

$orderbydate = DB::table('sales_flat_orders as w')
                ->select(array(DB::Raw('sum(w.total_item_count) as Day_count'),DB::Raw('DATE(w.created_at) day')))
                ->groupBy('day')
                ->orderBy('w.created_at')
                ->get();

我的查询中的Raw查询对于created_at字段是错误的。现在很好。希望你也能得到一些意见。

答案 1 :(得分:0)

$orderbydate = DB::table('sales_flat_orders as w')
                ->select(array(DB::Raw('sum(w.total_item_count) as 
                Day_count'),DB::Raw('DATE(w.created_at) as created_at')))
                ->orderBy('w.created_at')
                ->get();


$orderbydate->groupBy('w.created_at');