我想计算与特定用户相关的现有记录的总和后两列的百分比。
SELECT SUM(total_marks) AS TotalMarks,
SUM(obt_marks) AS ObtainedMarks,
TotalMarks/ObtainedMarks*100 As Percentage
FROM ExamResult WHERE (student_id = '5')
答案 0 :(得分:0)
一旦定义了列别名,就不能使用。而是重复以下表达式:
SELECT SUM(total_marks) AS TotalMarks,
SUM(obt_marks) AS ObtainedMarks,
SUM(total_marks) * 100.0 / SUM(obt_marks) As Percentage
FROM ExamResult
WHERE student_id = 5;
注意:
5
周围的单引号。大概id列是数字,而不是字符串。 (如果是字符串,请保留引号)。100
上加了一个小数点。如果列碰巧是整数,则可以防止整数除法。