我在表中有这样的数据:

=======================================
class |  name  |  study  |  avg scores
=======================================
1      alfa       c#          75
1      alfa       php         70
1      beta       c#          80
1      beta       php         90
2      carlie     c#          70
2      carlie     php         70
2      delta      c#          90
2      delta      php         90


我想要这样的结果:

==========================================
rangking |   class    |   name   | average
==========================================
1             1           beta       85
2             1           alfa       72.5
1             2           delta      90
2             2           carlie     70


评估标准1 =平均值> 80

范围为2的标准=平均值> 65和
那么如何在mysql中进行查询以获得这些结果呢?

请帮助我,我已经尝试了很多次,但是没有成功

谢谢

最佳答案

SELECT CASE WHEN average > 80 THEN 1
            WHEN average > 65 THEN 2
       END AS rangking,
       class, name, average
FROM (SELECT class, name, AVG(avg_scores) AS average
      FROM YourTable
      GROUP BY class, name
      HAVING average > 65) AS subquery
ORDER BY class, rangking

关于mysql - 查询在mysql中使用条件查找排名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24710363/

10-13 06:44