我有三张桌子之间的关系。我需要计算参与调查的每个人的百分比分数。我的表结构如下:
survey_answer_id
USER_ID
question_idanswer_voted
possible_answer_id
question_id
回答
question_id
问题
question_group_id
当某人收到调查问题时,答案会以surveyanswers
表格answer_voted
发布。 answer_voted
的值与possible_answer_id
相同,possibleanswers
是{{1}}表中的主键。
答案 0 :(得分:0)
所以我终于成功了。感谢你guyz..i我会在这里发布我的代码只是因为任何人发现类似的问题......很棒的一天伙伴们!
SELECT sa.userid
,q.question_group_id
,sum(pa.answer)/(select
count(q.question)*3)*100 as 'Percentage'
FROM surveyanswers sa
INNER JOIN possibleanswers pa
on pa.possible_answer_id = sa.answer_voted
INNER JOIN questions q
on q.question_id = sa.question_id
where userid=7
and q.question_group_id=1;