如何在Laravel中使用加减运算符构建sum()查询?

时间:2019-04-29 07:13:34

标签: mysql laravel

所以我有这样的查询;

kubectl exec simplehttp-777fd86759-w79pn -- cp app.py /tmp/ **flags**

如何在Laravel中构建它?我不知道如何查询“-” 减去函数,直到select sum(points_add) as total_add, sum(points_subtract) as total_deducts, (sum(points_add)-sum(points_subtract)) as total_balance from tbl_points where user_id = 2; 为止都知道如何做。

1 个答案:

答案 0 :(得分:2)

为此,最简单的方法是使用selectRaw()

$values = DB::table('tbl_points')
    ->selectRaw('sum(points_add) as total_add, sum(points_subtract) as total_deducts, (sum(points_add)-sum(points_subtract)) as total_balance')
    ->where('user_id', 2)
    ->first();

Laravel确实带有aggregates(),但是,它们用于返回单个值而不是多个聚合。