我想用下面的别名列来构建东西,但是我不知道如何使它成为Laravel查询。以下是我的SQL。
SELECT
d.*,
d.damaged_building,
d.total_victim
FROM
(
SELECT
delete_flg,
damage_id,
create_time,
reg_user_name,
municipality_id,
report_time,
district,
village,
disaster_type,
cause_of_disaster,
(
bd_major_damage1 + bd_minor_damage1 + bd_major_damage2 + bd_minor_damage2 + bd_major_damage3 + bd_minor_damage3
)
as damaged_building,
(
hi_hd_deaths + hi_hd_serious_injuries + hi_hd_minor_injuries + hi_hd_missing_persons + hi_hd_sick_persons
)
as total_victim
FROM
d_damage
WHERE
delete_flg = 0
ORDER BY
create_time DESC
)
d
我想将此翻译为Laravel Query Builder的Eloquent,以便在我的控制器中使用。
答案 0 :(得分:0)
如果我只是因为要把它带到任何地方,就只能坚持学习SQL查询,您可以学习很多框架/工具,而不是花时间在Laravel上。无论如何,您仍可以使用原始表达式在laravel中使用sql查询:https://laravel.com/docs/5.8/queries#raw-expressions
答案 1 :(得分:0)
您可以尝试
DB::table('d_damage')
->where('delete_flg',0)
->orderBy('create_time', 'desc')
->select('delete_flg', 'damage_id', 'create_time', 'reg_user_name', 'municipality_id', 'report_time', 'district', 'village', 'disaster_type', 'cause_of_disaster',DB::raw('(bd_major_damage1 + bd_minor_damage1 + bd_major_damage2 + bd_minor_damage2 + bd_major_damage3 + bd_minor_damage3) as damaged_building'),DB::raw('(hi_hd_deaths + hi_hd_serious_injuries + hi_hd_minor_injuries + hi_hd_missing_persons + hi_hd_sick_persons) as total_victim'))
->get();
这可能不完全是,但几乎是