【问题标题】:How to count where using DB::raw() in laravel?如何计算在 laravel 中使用 DB::raw() 的位置?
【发布时间】:2019-11-28 00:30:09
【问题描述】:

我有这个查询,但它不起作用

$order = Order::select('*', DB::raw('count(*) as num_product'),
DB::raw('count(status) where status = 1 as accepted')) // ERROR HERE
->groupBy(DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d')"))->get();

我想显示接受的订单数量,例如:2 of 5

【问题讨论】:

  • 为什么你不使用简单的 Laravel Eloquent:$order = Order::where('status', 1)->count();

标签: php mysql laravel


【解决方案1】:
$order = Order::select(DB::raw('count(*) as num_product, status'))
    ->where('status', 1)
    ->groupBy('status')
    ->groupBy(DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d')"))
    ->get();

【讨论】:

  • 我有4个数据,我需要按日期分组,然后显示状态是否被接受
猜你喜欢
  • 1970-01-01
  • 2015-10-27
  • 2015-11-20
  • 2018-10-24
  • 2023-03-24
  • 2018-01-18
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多