php - 这个复杂的SQL语句怎么写

标签 php mysql sql join subquery

我的 MySQL 表中有三个相同的表,即

first_term_result、second_term_result 和 third_term_result

这是其中的列

exam_type_id | student_id  | subject_id  | mark  |

或者使用虚拟数据的例子

注意:每个科目都有三种不同的考试类型(CA1、CA2、CA3 和考试), 有 3 个这样的表,内容相同但数据不同,因为它保存第一个学期的数据,另一个保存第二个学期的数据,最后一个保存第三个学期的数据。

first_term_result:

exam_type_id | student_id  | subject_id  | mark  |
    1        |    6        |     7       | 12    |
    2        |    6        |     7       | 9     |
    3        |    6        |     7       | 13    |   
    4        |    6        |     7       | 45    |
    1        |    4        |     7       | 7     |
    2        |    4        |     7       | 5     |
    3        |    4        |     7       | 10    |   
    4        |    4        |     7       | 34    |

second_term_result:

exam_type_id | student_id  | subject_id  | mark  |
    1        |    6        |     7       | 15    |
    2        |    6        |     7       | 6     |
    3        |    6        |     7       | 10    |   
    4        |    6        |     7       | 50    |
    1        |    4        |     7       | 6     |
    2        |    4        |     7       | 3     |
    3        |    4        |     7       | 9     |   
    4        |    4        |     7       | 44    |


third_term_result:

exam_type_id | student_id  | subject_id  | mark  |
    1        |    6        |     7       | 17    |
    2        |    6        |     7       | 8     |
    3        |    6        |     7       | 15    |   
    4        |    6        |     7       | 67    |
    1        |    4        |     7       | 12    |
    2        |    4        |     7       | 8     |
    3        |    4        |     7       | 12    |   
    4        |    4        |     7       | 50    |

现在我想要实现的是获取 first_term_result.mark second_term_result.markthird_term_result.mark 的 SUM() 每个学生 WHERE subject_id=7 按学生姓名分组。

另一个非常重要的问题是我将计算每个学生第一学期+第二学期+第三学期的总和,并且还希望能够为该学生和 DESC 中的科目排序总计,以便我可以相应地定位它们如果在 PHP 上会更容易,请告诉我。

谢谢

这对我来说似乎很复杂,但我知道这里有专家可以实现这一点,我在某处读到即使使用汇总也可以订购。

下面是我的代码,但显然不起作用。

SELECT CONCAT(s.fname,' ',s.mname,' ',s.lname) AS sname, 
SUM(f.mark) AS first_total, 
SUM(se.mark) AS second_total, 
SUM(t.mark) AS third_total 
SUM(f.first_total,second.total,third_total) as GT // just to show my intention 
FROM students s, first_term_result f, second_term_result se, third_term_result t 
WHERE s.studentID=f.student_id AND
s.studentID=se.student_id AND
s.studentID=t.student_id AND
f.subject_id=7 AND
se.subject_id=7 AND
t.subject_id=7
GROUP BY sname ORDER BY GT DESC

最佳答案

SELECT CONCAT(MS.fname,' ',MS.mname,' ',MS.lname) AS sname, 
  M.FTotal, M.STotal, M.TTotal, 
  (M.FTotal + M.STotal + M.TTotal) AS GrandTotal
FROM (
  SELECT st.studentID, 
     (
       SELECT SUM(f.mark) 
       FROM first_term_result AS f 
       WHERE f.subject_id = 7 
       AND f.student_id = st.studentID
     ) AS FTotal, 
     (
       SELECT SUM(s.mark) 
       FROM second_term_result AS s 
       WHERE s.subject_id = 7 
       AND s.student_id = st.studentID
     ) AS STotal, 
     (
       SELECT SUM(t.mark) 
       FROM third_term_result AS t 
       WHERE t.subject_id = 7 
       AND t.student_id = st.studentID
     ) AS TTotal
  FROM students AS st
  WHERE st.studentID IN (
    SELECT studentID 
    FROM first_term_result AS fs 
    WHERE fs.subject_id = 7 
    AND fs.student_id = st.studentID)
  GROUP BY st.studentID
) AS M
JOIN students MS ON M.studentID = MS.studentID
ORDER BY (M.FTotal + M.STotal + M.TTotal) DESC

关于php - 这个复杂的SQL语句怎么写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19451065/

相关文章:

php - 使用 PHP 解析 "advanced"INI 文件

mysql - 当用户单击表格行时显示详细信息

sql - SSMS - MYSQL Server 查询选项设置为开/关以短日期格式显示所有列?

php - 我们可以在同一页面的另一种表单中使用来自一种表单的用户输入数据吗

javascript - Vue JS 在 console.log 上返回 [Object object] 并在 JSON.parse 上返回未定义

javascript - 尝试编写一个基本的 echo 函数来检查 mysql 表

sql - 查找没有匹配条件的记录

mysql - FROM_UNIXTIME() 不工作 mysql

php - 评估存储在数组中的 PHP 逻辑

php - 数据加密传入Mysql数据库再由PHP读取