gpt4 book ai didi

php - 如何在 laravel 中使用求和运算符选择一个案例

转载 作者:行者123 更新时间:2023-11-30 21:29:14 24 4
gpt4 key购买 nike

我有这个查询,当不同列的值处于事件状态时,如 id_test = 1 note_test = 11,它对来自不同列的值求和,如果 id 在 0 中,它有一个 case stament

这是查询

select                                              
case when id_test1 = 1 then note_test1 else 0 end +
case when id_test2 = 1 then note_test2 else 0 end +
case when id_test3 = 1 then note_test3 else 0 end +
case when id_test4 = 1 then note_test4 else 0 end +
case when id_test5 = 1 then note_test5 else 0 end +
case when id_test6 = 1 then note_test6 else 0 end +
case when id_test7= 1 then note_test7 else 0 end as total_weighted
from rp_student where id = 1

这是我在 laravel 中的查询

$weighted= DB::table('rp_student')
->select(DB::raw('case when id_test1 = 1 then note_test1 else 0 end +
case when id_test2 = 1 then note_test2 else 0 end +
case when id_test3 = 1 then note_test3 else 0 end +
case when id_test4 = 1 then note_test4 else 0 end +
case when id_test5 = 1 then note_test5 else 0 end +
case when id_test6 = 1 then note_test6 else 0 end +
case when id_test7= 1 then note_test7 else 0 end as total_weighted'))
->where('id','=',1);

它没有返回任何值

最佳答案

您的查询末尾缺少必需的 ->get()。您当前看不到值的原因是查询尚未实际执行。

The get method returns an Illuminate\Support\Collection containing the results where each result is an instance of the PHP stdClass object. You may access each column's value by accessing the column as a property of the object:

https://laravel.com/docs/5.8/queries#retrieving-results

关于php - 如何在 laravel 中使用求和运算符选择一个案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57210476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com