如何在单个SQL语句中实现此目的

时间:2013-10-08 16:04:08

标签: php mysql pivot pivot-table

请我手头有问题,

我的数据库MySQL中有以下表格

students

studentID | fname   | mname | lname  |
1         | Wilson  | Dale  | Micheal|



examtype

examtypeID | name |
1          | ca 1
2          | ca 2
3          | ca 3
4          | exam


first_term_result

student_id | examtype_id | subject_id | mark
1          | 1           | 7          | 12
1          | 2           | 7          | 10
1          | 3           | 7          | 9
1          | 4           | 7          | 45

目前我有这个SQL语句

SELECT CONCAT(students.fname,' ',students.mname,' ',students.lname) 
    AS student_name, students.studentID, exam_type.name, first_term_result.mark 
    FROM students, exam_type, first_term_result 
    WHERE students.studentID=first_term_result.student_id 
    AND exam_type.exam_typeID=first_term_result.exam_type_id 
    AND first_term_result.subject_id=7

它起作用,因为我得到一个表格,我可以使用它来获得这样的数组

我是如何得到那个数组的 使用codeigniter框架:

    $sheet = $this->teacher_model->spreadsheet(); 
    $data = array();
    foreach($sheet as $row)
    {
        $data[$row['student_name']]['studentID']=$row['studentID'];
        $data[$row['student_name']][$row['name']]=$row['mark'];

    }


Array
(
  [Wilson Dale Micheal] => Array
    (
        [studentID] => 1
        [CA 1] => 12
        [CA 2] => 10
        [CA 3] => 9
        [Exam] => 45
    )

)

另一个SQL语句如下:

SELECT CONCAT(students.fname,' ',students.mname,' ',students.lname) 
    AS students_name, students.studentID, SUM(first_term_result.mark) as Total
    FROM students, first_term_result 
    WHERE students.studentID=first_term_result.student_id 
    AND first_term_result.subject_id=7 group by students_name

给出这样的东西:

Array
(
    [Wilson Dale Micheal] => Array
    (
        [studentID] => 1
        [Total] => 76
    )

)

合并两个数组我得到了我的结果,即:

这是我如何合并两个合并的功能     array_merge() 没用,所以我用过:

 $data = array();
    foreach($sheet as $row)
    {
        $data[$row['student_name']]['studentID']=$row['studentID'];
        $data[$row['student_name']][$row['name']]=$row['mark'];

    }

    foreach($total as $tol)
    {
        $data[$tol['student_name']]['Total']=$tol["Total"];
    }

Array
(
    [Wilson Dale Micheal] => Array
    (
        [studentID] => 1
        [CA 1] => 12
        [CA 2] => 10
        [CA 3] => 9
        [Exam] => 45
        [Total] => 76
    )

)

但我想知道我是否可以使用单个SQL语句实现所有这些,这样我就可以进行ORDER BY Total DESC,因此得分最高的学生将首先出现,依此类推。 当前的声明并没有将Total排序为DESC,尽管我拥有的数组就像我想要的那样。

谢谢

1 个答案:

答案 0 :(得分:1)

SELECT CONCAT(s.fname,' ',s.mname,' ',s.lname) AS student_name,        
       MAX(s.studentID) As StudentID,
       e.name As Name, 
       SUM(f.mark) As Mark
FROM Students s 
JOIN First_Term_Result f ON s.studentID = f.student_id 
JOIN Exam_Type e ON e.examtypeID = f.examtype_id
WHERE f.subject_id=7
GROUP BY CONCAT(s.fname,' ',s.mname,' ',s.lname), e.name WITH ROLLUP;

基于您的样本数据的结果将是:

STUDENT_NAME    STUDENTID   NAME    MARK
Wilson Dale Micheal    1    ca 1    12
Wilson Dale Micheal    1    ca 2    10
Wilson Dale Micheal    1    ca 3    9
Wilson Dale Micheal    1    exam    45
Wilson Dale Micheal    1    (null)  76
(null)                 1    (null)  76

ROLLUP添加了两行:

  • 学生总数(将针对每个学生出现)
  • 总计(所有学生一次)

如果你的MySQL版本支持GROUPING功能,你可以在“总”行中拥有自己的标签而不是NULL。

这里是SQL Fiddle,您可以通过修改表格内容进行测试。