查询小于和大于where子句中的语句

时间:2013-07-23 08:44:25

标签: php mysql

SELECT ( Sum(r.marks_attained) / Sum(r.total_marks) * 100 )AS marks,
       s.name
FROM   result r,
       student s
WHERE  r.student_id = s.student_id
GROUP  BY r.student_id
AND 
ORDER  BY marks DESC  

我如何才能获得百分比大于90的学生分数?

我可以在where子句中使用标记吗?

2 个答案:

答案 0 :(得分:2)

试试这个:

select (sum(r.marks_attained)/sum(r.total_marks)*100)as marks,s.name
from result r ,student s
where r.student_id=s.student_id 
group by r.student_id 
having (sum(r.marks_attained)/sum(r.total_marks)*100) >= 90
order by marks desc

我通过你的查询删除了where和group by之间的AND,所以我添加了一个having子句

P.S:> = 90(如果你想包括90,>否则为90)

答案 1 :(得分:0)

不,你不能在where子句中使用标记。因为它是一个聚合函数。它只能用于having子句。

SELECT (sum(r.marks_attained)/sum(r.total_marks)*100) as marks,
    s.name from result r, student s
WHERE
    r.student_id=s.student_id
GROUP BY r.student_id
HAVING
    (sum(r.marks_attained)/sum(r.total_marks)*100) > 90
ORDER BY marks DESC

SELECT (sum(r.marks_attained)/sum(r.total_marks)*100) as marks,
   s.name from result r,
   student s
WHERE
   r.student_id=s.student_id 
GROUP BY r.student_id
HAVING marks > 90
ORDER BY marks DESC