Laravel 添加计算值在另一个 table 中的总和

Laravel add calculate the sum where the value is in another table

基本上我有两个table。 answerschoices 。在我的 choices table 中,我得到一个列 choices.value,它的值是 0-4。我当前的查询是这样的

 $answers = \DB::table('answers')
                    ->join('choices','answers.choice_id','=','choices.choice_id')
                    ->select('answers.user_id','choices.choice_id','choices.choice','choices.value')
                    ->where('answers.user_id',\Auth::user()->id)
                    //->groupBy('answers.user_id')
                    ->get();

我现在的回复是这样的

    "user_id": 2,
        "choice_id": 2,
        "choice": "I feel discourated about the future",
        "value": 1
    },
    {
        "user_id": 2,
        "choice_id": 2,
        "choice": "I don't enjoy things the way I used to",
        "value": 1
    },
    {
        "user_id": 2,
        "choice_id": 2,
        "choice": "I feel guilty a good part of time",
        "value": 1

如何添加值才能使我的结果像这样

"user_id":2,
"total_score":3

我试过 DB::raw(SUM(choices.values)as score) 但我得到了很多。我猜它添加了选项 table 中的所有选项值,而不是答案中的值。

我的答案数据库,我只有 select 用户的答案 = 2。我限制为 5

+---------+-------------+-----------+
| user_id | question_id | choice_id |
+---------+-------------+-----------+
|       2 |           1 |         2 |
|       2 |           2 |         2 |
|       2 |           3 |         2 |
|       2 |           4 |         2 |
|       2 |           5 |         2 |
+---------+-------------+-----------+

我的选择table,我只select问题1和2及其选择。

+-----------+-------------+--------------------------------------------------------------+-------+
| choice_id | question_id | choice                                                       | value |
+-----------+-------------+--------------------------------------------------------------+-------+
|         1 |           1 | I do not feel sad                                            |     0 |
|         2 |           1 | I feel sad                                                   |     1 |
|         3 |           1 | I am sad all the time and I can't snap out of it             |     2 |
|         4 |           1 | I am so sad and unhappy that I can't stand it                |     3 |
|         1 |           2 | I am not particularly discouraged about the future           |     0 |
|         2 |           2 | I feel discourated about the future                          |     1 |
|         3 |           2 | I feel I have nothing to look forward to                     |     2 |
|         4 |           2 | I feel the future is hopeless and that things cannot improve |     3 |
+-----------+-------------+--------------------------------------------------------------+-------+

我还想创建一个名为 scores 的新 table,这些列将是我想要的结果。我想在每个 answers.user_id 的答案中添加 choices.values ,这样当我查看分数 table 时,它将显示每个用户的总分或当用户完成所有 [= =26=] 问题因为我只有 21 项它会自动添加到 scores table。我可以那样做吗?在choice_id的基础上在answerstable中加一个value可以吗?这就是我的想法,但我认为它是多余的,因为 choice_id 已经存在了。提前致谢。

PS:尝试编写这些查询但总是得到 441,这是 choices table[= 中所有选项的总数 value 35=]

SELECT  answers.user_id,choices.choice_id,choices.value,COALESCE(sum(choices.value),0) as score  FROM 
`answers`  JOIN `choices` ON  choices.choice_id = answers.choice_id where 
answers.user_id = 2

SELECT answers.user_id,choices.choice_id,choices.value,SUM(choices.value),0 as score FROM `answers`
join choices on choices.choice_id = answers.choice_id
where answers.user_id = 2

SELECT answers.user_id,choices.choice_id, sum(choices.value) from answers 
JOIN `choices` ON  choices.choice_id = answers.choice_id
group by answers.user_id 

使用分组可能会有帮助

 $answers = \DB::table('answers')
                    ->join('choices','answers.choice_id','=','choices.choice_id')
                    ->select('answers.user_id',DB::raw('count(*) as total'))
                    ->where('answers.user_id',\Auth::user()->id)
                    ->groupBy('answers.user_id')
                    ->get();

目前您只匹配一个选项,但您需要匹配选项和问题。

我在看到每个 choices.question_id 在闲聊中选择 1、2、3 和 4 之后发现了这个。直到我真正看到了选项的屏幕截图,我才知道 table。

$answers = \DB::table('answers')
                    ->join('choices', function ($q) {
                    $q->on('answers.choice_id','=','choices.choice_id')
                    ->on('answers.question_id', '=', 'choices.question_id');
                    })
                    ->select('answers.user_id',\DB::raw('sum(choices.value) as total'))
                    ->groupBy('answers.user_id')
                    ->get();