Php MySQL按类别划分的平均分数

Php MySQL按类别划分的平均分数,php,mysql,Php,Mysql,我再一次陷入了mysql查询的困境,除非我在其他查询中编写了许多查询,否则我无法理解它 我有10个专栏。5个调查答案栏,5个答案类别栏 qone_answer, qone_answer_cat qtwo_answer, qtwo_answer_cat qthree_answer, qthree_answer_cat qfour_answer, qfour_answer_cat qfive_answer, qfive_answer_cat 共有5个类别。创建调查时,将为5个问题中的每一个选择一个

我再一次陷入了mysql查询的困境,除非我在其他查询中编写了许多查询,否则我无法理解它

我有10个专栏。5个调查答案栏,5个答案类别栏

qone_answer, qone_answer_cat
qtwo_answer, qtwo_answer_cat
qthree_answer, qthree_answer_cat
qfour_answer, qfour_answer_cat
qfive_answer, qfive_answer_cat
共有5个类别。创建调查时,将为5个问题中的每一个选择一个类别

也就是说,问题1可能是第4类-问题2可能是第2类。等等等等

我的任务是查询表并获得5个类别的平均评分

我能做到这一点的唯一方法是对每个答案和每个答案类别执行一个查询,但这将是25个查询,这不是最有效的方法

我的想法是:

  $slot_one_category_one = "SELECT AVG(answer_one) as slot_one_answer_one_avg FROM response_storage WHERE answer_one_category='1'";
  $slot_two_category_one = "SELECT AVG(answer_one) as slot_two_answer_one_avg FROM response_storage WHERE answer_two_category='1'";
  $slot_three_category_one = "SELECT AVG(answer_one) as slot_two_answer_one_avg FROM response_storage WHERE answer_three_category='1'";
每个插槽和每个cat的等

最后,我需要找到每一个等于1的答案类别,并得到答案,存储它,然后继续循环。。然后对每个答案类别(等于2、3、4、5)执行此操作,总共25个查询

有人能帮我找到更好的方法吗

这是一个早期的尝试做了另一种方式,但它确实得到了我试图做的重点。本例中的列名略有不同,但想法相同。。问题和问题类别:

$survey_category_totals = mysql_query("SELECT question_one,qone_cat FROM survey WHERE clinic_id='$clinic_id'");
while($row = mysql_fetch_array($survey_category_totals))
{
$cat_question_one = $row['question_one'];
$cat_cat_one = $row['qone_cat'];
    if ($cat_cat_one==1){
        $category_one_total=$category_one_total+$cat_question_one;
        $category_one_count=$category_one_count+1;          
    }
    if ($cat_cat_one==2){
        $category_two_total=$category_two_total+$cat_question_one;
        $category_two_count=$category_two_count+1;          
    }
    if ($cat_cat_one==3){
        $category_three_total=$category_three_total+$cat_question_one;
        $category_three_count=$category_three_count+1;          
    }
    if ($cat_cat_one==4){
        $category_four_total=$category_four_total+$cat_question_one;
        $category_four_count=$category_four_count+1;            
    }
    if ($cat_cat_one==5){
        $category_five_total=$category_five_total+$cat_question_one;
        $category_five_count=$category_five_count+1;            
    }                               
}
$survey_category_totals = mysql_query("SELECT question_two,qtwo_cat FROM survey WHERE clinic_id='$clinic_id'");
while($row = mysql_fetch_array($survey_category_totals))
{
$cat_question_one = $row['question_two'];
$cat_cat_one = $row['qtwo_cat'];
    if ($cat_cat_one==1){
        $category_one_total=$category_one_total+$cat_question_one;
        $category_one_count=$category_one_count+1;          
    }
    if ($cat_cat_one==2){
        $category_two_total=$category_two_total+$cat_question_one;
        $category_two_count=$category_two_count+1;          
    }
    if ($cat_cat_one==3){
        $category_three_total=$category_three_total+$cat_question_one;
        $category_three_count=$category_three_count+1;          
    }
    if ($cat_cat_one==4){
        $category_four_total=$category_four_total+$cat_question_one;
        $category_four_count=$category_four_count+1;            
    }
    if ($cat_cat_one==5){
        $category_five_total=$category_five_total+$cat_question_one;
        $category_five_count=$category_five_count+1;            
    }                               
}   
$survey_category_totals = mysql_query("SELECT question_three,qthree_cat FROM survey WHERE clinic_id='$clinic_id'");
while($row = mysql_fetch_array($survey_category_totals))
{
$cat_question_one = $row['question_three'];
$cat_cat_one = $row['qthree_cat'];
    if ($cat_cat_one==1){
        $category_one_total=$category_one_total+$cat_question_one;
        $category_one_count=$category_one_count+1;          
    }
    if ($cat_cat_one==2){
        $category_two_total=$category_two_total+$cat_question_one;
        $category_two_count=$category_two_count+1;          
    }
    if ($cat_cat_one==3){
        $category_three_total=$category_three_total+$cat_question_one;
        $category_three_count=$category_three_count+1;          
    }
    if ($cat_cat_one==4){
        $category_four_total=$category_four_total+$cat_question_one;
        $category_four_count=$category_four_count+1;            
    }
    if ($cat_cat_one==5){
        $category_five_total=$category_five_total+$cat_question_one;
        $category_five_count=$category_five_count+1;            
    }                               
}   
$survey_category_totals = mysql_query("SELECT question_four,qfour_cat FROM survey WHERE clinic_id='$clinic_id'");
while($row = mysql_fetch_array($survey_category_totals))
{
$cat_question_one = $row['question_four'];
$cat_cat_one = $row['qfour_cat'];
    if ($cat_cat_one==1){
        $category_one_total=$category_one_total+$cat_question_one;
        $category_one_count=$category_one_count+1;          
    }
    if ($cat_cat_one==2){
        $category_two_total=$category_two_total+$cat_question_one;
        $category_two_count=$category_two_count+1;          
    }
    if ($cat_cat_one==3){
        $category_three_total=$category_three_total+$cat_question_one;
        $category_three_count=$category_three_count+1;          
    }
    if ($cat_cat_one==4){
        $category_four_total=$category_four_total+$cat_question_one;
        $category_four_count=$category_four_count+1;            
    }
    if ($cat_cat_one==5){
        $category_five_total=$category_five_total+$cat_question_one;
        $category_five_count=$category_five_count+1;            
    }                               
}   
$survey_category_totals = mysql_query("SELECT question_five,qfive_cat FROM survey WHERE clinic_id='$clinic_id'");
while($row = mysql_fetch_array($survey_category_totals))
{
$cat_question_one = $row['question_five'];
$cat_cat_one = $row['qfive_cat'];
    if ($cat_cat_one==1){
        $category_one_total=$category_one_total+$cat_question_one;
        $category_one_count=$category_one_count+1;          
    }
    if ($cat_cat_one==2){
        $category_two_total=$category_two_total+$cat_question_one;
        $category_two_count=$category_two_count+1;          
    }
    if ($cat_cat_one==3){
        $category_three_total=$category_three_total+$cat_question_one;
        $category_three_count=$category_three_count+1;          
    }
    if ($cat_cat_one==4){
        $category_four_total=$category_four_total+$cat_question_one;
        $category_four_count=$category_four_count+1;            
    }
    if ($cat_cat_one==5){
        $category_five_total=$category_five_total+$cat_question_one;
        $category_five_count=$category_five_count+1;            
    }                               
}

您实际上只需要5个查询,联合在一起就可以成为一个大查询

SELECT AVG(answer_one) AS slot_one, answer_one_category
FROM response_storage
GROUP BY answer_one_category
UNION ALL
SELECT AVG(answer_two) AS slot_two, answer_two_category
FROM response_storage
GROUP BY answer_two_category
UNION ALL
SELECT AVG(answer_three) AS slot_three, answer_three_category
FROM response_storage
GROUP BY answer_three_category
UNION ALL
SELECT AVG(answer_four) AS slot_four, answer_four_category
FROM response_storage
GROUP BY answer_four_category
UNION ALL
SELECT AVG(answer_five) AS slot_five, answer_five_category
FROM response_storage
GROUP BY answer_five_category
这将返回两列。每个时段/类别组合的平均答案,应为25条记录

如果您只需要按类别列出平均答案,那么您可以将每个问题的每个答案和类别合并,然后取平均值:

SELECT
    category,
    avg(answer) average_answer
FROM
    (
        SELECT answer_one as answer, answer_one_category as category FROM response_storage      
        UNION ALL
        SELECT answer_two as answer, answer_two_category as category FROM response_storage      
        UNION ALL
        SELECT answer_three as answer, answer_three_category as category FROM response_storage      
        UNION ALL
        SELECT answer_four as answer, answer_four_category as category FROM response_storage        
        UNION ALL
        SELECT answer_five as answer, answer_five_category as category FROM response_storage        
    )subtbl
GROUP BY 1

有趣的是,这里的UNION实际上将结果放在了一个更适合于表模式的格式中(这就是为什么感觉如此痛苦的原因)。一个好的模式应该是
QuestionNumber | Category | Answer
,它可以使您的请求变得简单,例如:
从response|u存储组中按类别选择avg(Answer),Category
。这还有一个额外的好处,即允许您在不改变表格结构的情况下在将来添加问题。

最后我需要5个数字。每个类别1个。在答案中添加更多内容以包含该内容。