[英]Laravel add calculate the sum where the value is in another table
基本上我有两张桌子。 answers
和choices
。 在我的choices
表中,我有一个列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)
但我得到了很多。 我想它添加了选择表中的所有选择值,而不是在答案中。
我的答案 db 我只选择用户的答案 = 2。我限制为 5
+---------+-------------+-----------+
| user_id | question_id | choice_id |
+---------+-------------+-----------+
| 2 | 1 | 2 |
| 2 | 2 | 2 |
| 2 | 3 | 2 |
| 2 | 4 | 2 |
| 2 | 5 | 2 |
+---------+-------------+-----------+
我的选择表,我只选择了问题 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
的新表,列将是我想要的结果。 我想在每个answers.user_id
的答案中添加choices.values
,这样当我查看分数表时,它会显示每个用户的总分或当用户完成所有21
问题的回答时,因为我只有 21 个项目将自动添加到scores
表中。 我可以这样做吗?。 可以在基于choice_id
的answers
表中添加一个value
吗? 这就是我的想法,但我认为它是多余的,因为choice_id
已经存在了。 提前致谢。
PS:试着写这些查询,但总是得到441
这是总value
都在的选择choices
表
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
使用 group by 也许它会有所帮助
$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 在一个slack 对话中有选择1、2、3 和4 之后发现了这一点。 直到我真正看到了选择表的屏幕截图,我才知道。
$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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.