laravel中选择多个表列和别名条件
Posted
技术标签:
【中文标题】laravel中选择多个表列和别名条件【英文标题】:Selecting multiple table columns and alias conditions in laravel 【发布时间】:2020-07-05 14:28:32 【问题描述】:我想在 laravel 中实现这个查询..
select sum(reading * multiplier) as consumption, reading_date, count(id) as num, (sum(reading * multiplier) / count(id)) as average
from electricities where meter_name != '' ;
到目前为止我有这个
electricities = Electricity::select(DB::raw('sum(reading * multiplier) as consumption', 'reading_date', 'count(id) as num', '(sum(reading * multiplier) / count(id)) as average'))
->where('factory_code', '=', $request->$factory_code)
->whereYear('reading_date', $request->$year)
->groupBy(DB::raw('month(reading_date)'))
->orderBy('reading_date', 'asc')
->get();
但它不会返回所需的输出..
谁能帮我解决这个问题?谢谢
【问题讨论】:
删除where
、whereYear
、groupBy
、orderBy
,并添加where('meter_name', '<>', '')
?
【参考方案1】:
根据您的查询:
electricities = Electricity::select(DB::raw('sum(reading * multiplier) as consumption', 'reading_date', 'count(id) as num', '(sum(reading * multiplier) / count(id)) as average'))
->where('meter_name', '!=', '')
->get();
【讨论】:
以上是关于laravel中选择多个表列和别名条件的主要内容,如果未能解决你的问题,请参考以下文章