groupby 不支持 laravel 和 PostreSQL
我正在使用 PostreSQL 。当我尝试在 Laravel 查询中使用 groupBy 时。它显示分组错误:
$available_Area = DB::table('area AS a')
->select('a.*','a.name as a_name', 'f.name as f_name', 'zc.code as zc_code')
->join('transaction AS t', 't.f_id', '=', 'a.f_id')
->leftjoin('facility AS f', 'f.id', '=', 't.f_id')
->leftJoin('transaction_detail AS td', 'td.t_id', '=', 't.id')
->leftJoin('zone_component AS zc', 'zc.id', '=', 'td.comp_id')
->whereNotIn('a.id', DB::table('transaction_detail AS td')
->select('zc.a_id')
->join('zone_component AS zc', 'zc.id', '=', 'td.comp_id')
->whereIn('td.card_type', ['C_OVA','C_M21'])
->where('td.t_id', $url)
)
->groupBy('a.id')
->where('t.id', @$url)
->get();
然后我尝试使用DISTINCT ON它显示语法错误
$available_Area = DB::table('area AS a')
->select('a.*','a.name as a_name', 'f.name as f_name', 'zc.code as zc_code', DB::raw("DISTINCT ON(a.id)"))
->join('transaction AS t', 't.f_id', '=', 'a.f_id')
->leftjoin('facility AS f', 'f.id', '=', 't.f_id')
->leftJoin('transaction_detail AS td', 'td.t_id', '=', 't.id')
->leftJoin('zone_component AS zc', 'zc.id', '=', 'td.comp_id')
->whereNotIn('a.id', DB::table('transaction_detail AS td')
->select('zc.a_id')
->join('zone_component AS zc', 'zc.id', '=', 'td.comp_id')
->whereIn('td.card_type', ['C_OVA','C_M21'])
->where('td.t_id', $url)
)
->where('t.id', @$url)
->get();
I am using PostreSQL . when I try to use groupBy in my laravel query. It showing Grouping error:
$available_Area = DB::table('area AS a')
->select('a.*','a.name as a_name', 'f.name as f_name', 'zc.code as zc_code')
->join('transaction AS t', 't.f_id', '=', 'a.f_id')
->leftjoin('facility AS f', 'f.id', '=', 't.f_id')
->leftJoin('transaction_detail AS td', 'td.t_id', '=', 't.id')
->leftJoin('zone_component AS zc', 'zc.id', '=', 'td.comp_id')
->whereNotIn('a.id', DB::table('transaction_detail AS td')
->select('zc.a_id')
->join('zone_component AS zc', 'zc.id', '=', 'td.comp_id')
->whereIn('td.card_type', ['C_OVA','C_M21'])
->where('td.t_id', $url)
)
->groupBy('a.id')
->where('t.id', @$url)
->get();
Then I tried with DISTINCT ON It showing syntax error
$available_Area = DB::table('area AS a')
->select('a.*','a.name as a_name', 'f.name as f_name', 'zc.code as zc_code', DB::raw("DISTINCT ON(a.id)"))
->join('transaction AS t', 't.f_id', '=', 'a.f_id')
->leftjoin('facility AS f', 'f.id', '=', 't.f_id')
->leftJoin('transaction_detail AS td', 'td.t_id', '=', 't.id')
->leftJoin('zone_component AS zc', 'zc.id', '=', 'td.comp_id')
->whereNotIn('a.id', DB::table('transaction_detail AS td')
->select('zc.a_id')
->join('zone_component AS zc', 'zc.id', '=', 'td.comp_id')
->whereIn('td.card_type', ['C_OVA','C_M21'])
->where('td.t_id', $url)
)
->where('t.id', @$url)
->get();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用
select a.*
,因此您应该将所有字段添加到group by语句或应用聚合函数。例如,将字段a.id
、a.name
、f.name
添加到分组依据,并在字段zc 上应用 min 函数。代码:
一些简化的示例
You can not use
select a.*
, so you should to add all fields into group by statement or apply aggregation function. For example fieldsa.id
,a.name
,f.name
added to group by and applied min function on fieldzc.code
:Some simplified example