MySQL按类别划分的平均分数

时间:2015-03-08 04:26:10

标签: 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个问题中的每个问题选择一个类别。

即。问题1可能是第4类 - 问题2可能是第2类。等等。

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

我能做到这一点的唯一方法是对每个答案和每个answer_category执行查询,但这将是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'";

等。每个插槽和每只猫的等等。

最后,我需要找到每个answer_category等于1,并得到答案,存储它,并继续循环..然后为每个answer_category执行此操作,等于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;            
    }                               
}

1 个答案:

答案 0 :(得分:0)

你真的只需要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,它会使您的请求变得简单,例如:SELECT avg(answer), category FROM response_storage GROUP BY category。这还有一个额外的好处,即允许您在将来添加问题而不改变您的表结构。