首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel查询。仅以雄辩的方式获取合计>0

Laravel查询。仅以雄辩的方式获取合计>0
EN

Stack Overflow用户
提问于 2019-06-18 00:32:08
回答 1查看 44关注 0票数 0

我有这样的疑问:

代码语言:javascript
复制
public static function totalByAgent(int $agentId)
{
    return PropertyListing::select(
        DB::raw('SUM(property_listing.rental) as rental'),
        DB::raw('SUM(property_listing.sale) as sale'),
        'property_category.name as category_name',
        'property_category.id as category_id'
    )->join(
        'property_category',
        'property_category.id',
        'property_listing.category_id'
    )->where('agent_id', $agentId)->groupBy('property_category.id')->get();
}

通过这个查询,我获得了待售房产的总和和按房产类别分组的出租房产的总和。但是,如果某些房产只有待售房产和可供出租的房产,那么我得到的租金总和为0。

我尝试添加:

代码语言:javascript
复制
->having('sale', '>', 0)->get()

在groupBy之后。这隐藏了租金,如果他们有的话。

有什么想法吗?

致以最美好的问候

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-18 15:31:28

最后,这个查询适用于我:

代码语言:javascript
复制
      return Property::published()->select(DB::raw('SUM(property.rental) as rental'),
                                    DB::raw('SUM(property.sale) as sale'),
                                    'property_category.name as category_name',
                                    'property_category.id as category_id')
          ->join('property_category', 'property_category.id', 'property.category_id')
          ->whereExists(function ($query) use ($agentId){
                      $query->select('property_listing.agent_id')
                            ->from('property_listing')
                            ->whereRaw('property.id = property_listing.property_id')
                            ->where('property_listing.agent_id', $agentId);
                            })->published()
         ->groupBy('property_category.id')->get();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56635453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档