我有这样的疑问:
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。
我尝试添加:
->having('sale', '>', 0)->get()在groupBy之后。这隐藏了租金,如果他们有的话。
有什么想法吗?
致以最美好的问候
发布于 2019-07-18 15:31:28
最后,这个查询适用于我:
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();https://stackoverflow.com/questions/56635453
复制相似问题