一行的总和(两列的平均值)和相同的行(另一列的值)

时间:2018-05-29 05:53:30

标签: mysql rdbms

This is the table I've created, name of table is elaborate_result

我已经写了一个查询,但它显示错误。我希望得到mid_1_englishmid_2_english的AVG,然后想要将{AVGG添加到semester_final_english

select s_roll_no, s_f_name, s_l_name, s_class, 
           SUM(mid_1_english + mid_2_english)/2 as mid_english, 
           SUM(mid_1_mathematics + mid_2_mathematics)/2 as mid_mathematics, 
           SUM(mid_english + semester_final_english) as total_english, 
           SUM(mid_mathematics + semester_final_mathematics) as total_mathematics 
from elaborate_result 
group by s_f_name, s_l_name;
LIMIT 0, 50000 
  

错误代码:1054。未知栏' mid_english'在'字段列表' 0.000   秒

1 个答案:

答案 0 :(得分:0)

当你做这样的事情时

    Select SUM(mid_1_english + mid_2_english)/2 as mid_english 

它不会创建新列mid_english。因此,您不能在同一查询中将其用作列。

所以你需要的东西可能看起来像这样

    select s_roll_no, s_f_name, s_l_name, s_class, 
       SUM(mid_1_english + mid_2_english)/2 as mid_english, 
       SUM(mid_1_mathematics + mid_2_mathematics)/2 as mid_mathematics, 
       SUM((mid_1_english + mid_2_english)/2 + semester_final_english) as 
     total_english, 
      SUM((mid_1_mathematics + mid_2_mathematics)/2 + 
     semester_final_mathematics) as total_mathematics 
    from elaborate_result 
  group by s_f_name, s_l_name;
 LIMIT 0, 50000