如何从多个SQL表中选择数据?

时间:2016-11-10 18:11:09

标签: sql

我有2张桌子,如下所述:

等级

student id, 
exem1, 
exam2, 
exam3

姓名

student id, 
names

我想显示学生的姓名,3门考试的平均成绩和字母成绩。字母等级计算如下:

90+ is an “A”80 - <90 will be a “B”,依此类推。我应该怎么做。?

2 个答案:

答案 0 :(得分:1)

SELECT n.NAME AS "Name", CASE WHEN ((g.exam1+g.exam2+g.exam3)/3) = 90 then 'A'
 WHEN ((g.exam1+g.exam2+g.exam3)/3) < 90 then 'B'
 ELSE 'C'
 END AS "Grade"
FROM NAMES AS n INNER JOIN GRADES AS g 
ON g.student_id = n.student_id

根据需要修改范围。

答案 1 :(得分:1)

> SELECT n.student_names, ((g.exam1+g.exam2+g.exam3)/3) AS 'AVERAGE_RESULT', (CASE 
              WHEN ((g.exam1+g.exam2+g.exam3)/3) =90  
                 THEN 'A' 
              WHEN (((g.exam1+g.exam2+g.exam3)/3) BETWEEN 80 AND 89)  
                 THEN 'B' 
              WHEN (((g.exam1+g.exam2+g.exam3)/3) BETWEEN 70 AND 79)  
                 THEN 'C' 
              ELSE 'D') END AS 'Student_grades',
FROM names n, grades g
WHERE n.students_id = g.students_id;

使用BETWEEN包含范围