laravel query builder如何对多个特定列求和

时间:2015-09-07 21:11:24

标签: php mysql laravel query-builder

你好,我有一个候选人总分数表

样品

enter image description here

我想添加casual_wear和evening_gown,其中候选ID = 1

到目前为止我得到了什么

DB::table('total_score')
->select(DB::raw('SUM(casual_wear) as total_scores'))
->where('candidate_number', '=' , '1')
->orderBy('total_scores', 'desc')
->get();

我不知道如何添加到不同的列pls help,tnx提前

1 个答案:

答案 0 :(得分:1)

  

我想添加casual_wear和evening_gown,其中候选ID = 1

那么为什么你不能在

这两个列上执行加法
select casual_wear + evening_gown as some_alias
from total_score
where candidate_ID =1;

(或)你可能打算做SUM() GROUP BY candidate_ID喜欢

select SUM(casual_wear) as total_scores,
SUM(evening_gown) as total_scores1
from total_score
GROUP BY candidate_ID
order By SUM(casual_wear) desc;