计算三个表之间的百分比

时间:2015-06-09 08:16:29

标签: php mysql

我有三张桌子之间的关系。我需要计算参与调查的每个人的百分比分数。我的表结构如下:

Surveyanswers

survey_answer_id
USER_ID
question_idanswer_voted

possibleanswers

possible_answer_id
question_id
回答

问题

question_id

问题

question_group_id

当某人收到调查问题时,答案会以surveyanswers表格answer_voted发布。 answer_voted的值与possible_answer_id相同,possibleanswers是{{1}}表中的主键。

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;