请问我手头有问题,
我的数据库 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 语句来实现所有这些,这样我就可以按总 DESC 进行排序,所以得分最高的学生将首先出现,依此类推。尽管我拥有我想要的数组,但当前语句不会将 Total 排序为 DESC。
谢谢