-1

我正在尝试在 Laravel 中进行查询,但它不起作用。我使用 POSTGRESQL

select extract(year from fecha_creacion) as anio, extract(month from fecha_creacion) as mes,
     sum(case when tipo = 'entrada' then 1 else 0 end ) 
  from documento 
  group by extract(year from fecha_creacion), extract(month from fecha_creacion)
  order by anio, mes;

我还尝试了以下方法。

$data = DB::table('documento')
    ->selectRaw('year from fecha_creacion AS anio, month from fecha_creacion AS mes')
    ->sum(case when tipo = 'entrada' then 1 else 0 end )
    ->orderBy(anio, mes, DESC)
    ->get();
4

1 回答 1

0

您可以使用 laravelDB::raw()表达式查询。

您必须在原始函数中传递查询。

参考:https ://laravel.com/docs/5.8/queries#raw-expressions

于 2019-06-16T20:09:57.603 回答