MySQL - 无法使用多个选择和加入选择所有科目和成绩

时间:2015-03-04 12:38:10

标签: mysql join

我的问题是关于MySQL数据库。我正在做一个学生管理系统, 我坚持一些查询,我想选择grade_point grade_name和subject_name。我有四张相关的表,但只有一张表没有关系,我想用它来比较标记。

所以我的期望是根据学生ID和考试ID显示所有科目,grade_point,grade_name。

以下是我的表格:

成绩

--------------------------------------------------------------------------------
|grade_id|grade_name|grade_point|grade_mark_from|grade_mark_up|comment         |
--------------------------------------------------------------------------------
|1       |A+        |5          |80             |100          |Very good result|
|2       |A         |4          |70             |79           |Good result     |
|3       |B+        |3.5        |60             |69           |Good result     |
|4       |B         |3          |50             |59           |Good            |
|5       |C         |2          |40             |49           |Good            |
|1       |D         |2.5        |30             |39           |Good            |
|1       |E         |1.5        |1              |33           |No good         |
--------------------------------------------------------------------------------

Exam_Result:


|resu_id|mark|comment|exam_id|subject_id|student_id|
----------------------------------------------------
|1      |50  | no    |1      |1         |1         |
|2      |30  | no    |1      |1         |2         |
|3      |70  | no    |1      |1         |1         |
|4      |90  | no    |1      |1         |2         |
|5      |60  | no    |1      |1         |1         |
|6      |25  | no    |1      |1         |2         |

主题:

 ---------------------------------------
|subject_id|subject_name|using_class_id|
----------------------------------------
|1         |Math        |1             |    
|2         |English     |1             |   
|3         |BL          |2             |   
|4         |Testing     |2             |    
----------------------------------------

表学生:

 ---------------------------------------
|student_id|student_name|using_class_id|
----------------------------------------
|1         |samphors    |1             |    
|2         |vann        |1             |   
|3         |Dyna        |2             |   
|4         |Khan        |2             |    
----------------------------------------

这是我的期望,但只是一个例子: enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个:

select s.student_name, su.subject_name, er.mark, (select gr.grade_mark_up from grade gr where er.mark >= gr.grade_mark_from and er.mark <= gr.grade_mark_up) Highest_Mark, (select gr.grade_name from grade gr where er.mark >= gr.grade_mark_from and er.mark <= gr.grade_mark_up) Grade from students s, exam_result er, subject su where s.student_id = er.student_id and er.subject_id = su.subject_id

年级等级_mark_up&#34; E&#34;是错的!