groupby 不支持 laravel 和 PostreSQL

发布于 2025-01-15 22:20:41 字数 1710 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-黛色若梦 2025-01-22 22:20:41

您不能使用select a.*,因此您应该将所有字段添加到group by语句或应用聚合函数。例如,将字段 a.ida.namef.name 添加到分组依据,并在字段 zc 上应用 min 函数。代码:

$available_Area = DB::table('area AS a')
    ->select('a.id','a.name as a_name', 'f.name as f_name', 'min(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', 'a.name', 'f.name')
    ->where('t.id', @$url) 
    ->get();

一些简化的示例

You can not use select a.*, so you should to add all fields into group by statement or apply aggregation function. For example fields a.id, a.name, f.name added to group by and applied min function on field zc.code:

$available_Area = DB::table('area AS a')
    ->select('a.id','a.name as a_name', 'f.name as f_name', 'min(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', 'a.name', 'f.name')
    ->where('t.id', @$url) 
    ->get();

Some simplified example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文