使用嵌套select子式 解决mysql不能叠加使用如max(sum())的问题

来源:互联网 发布:优盘数据恢复实例 编辑:程序博客网 时间:2024/05/18 02:09

网上也有解决方案 有的有瑕疵 有的较复杂(mysql没有分析函数,可以使用变量实现)

  1. select sumScoreValue,studentid,studentName from sc_student b,  
  2. (select sum(scoreValue) as sumScoreValue, studentid   
  3. from sc_score group by studentid       
  4. order by sumScoreValue desc limit 1) as a  
  5. where a.studentid=b.studentNo  
//这样做 只能查询第一名只有一个的情况 很巧妙

  1. mysql> select studentid,scoreValue from sc_score;  
  2. +-----------+------------+  
  3. | studentid | scoreValue |  
  4. +-----------+------------+  
  5. |         1 |         80 |  
  6. |         1 |         85 |  
  7. |         1 |         90 |  
  8. |         2 |         75 |  
  9. |         2 |         80 |  
  10. |         2 |         84 |  
  11. |         3 |         85 |  
  12. |         3 |         85 |  
  13. |         3 |         85 |  
  14. +-----------+------------+  
  15. rows in set (0.00 sec)  
  16.   
  17. mysql> SELECT studentNo,studentName FROM sc_student;  
  18. +-----------+-------------+  
  19. | studentNo | studentName |  
  20. +-----------+-------------+  
  21. |         1 | aa          |  
  22. |         2 | bb          |  
  23. |         3 | cc          |  
  24. +-----------+-------------+  
  25. rows in set (0.00 sec)  
  26.   
  27.   
  28. mysql> SELECT a.studentid,  
  29.     ->        b.studentName,  
  30.     ->        a.sumScoreValue  
  31.     ->   FROM (SELECT tmp.studentid,  
  32.     ->                tmp.sumScoreValue,  
  33.     ->                IF(@groupid = tmp.sumScoreValue,@rank := 1,@rank := @rank + 1) AS rank,  
  34.     ->                @groupid := tmp.sumScoreValue  
  35.     ->           FROM (SELECT studentid,  
  36.     ->                        SUM(scoreValue) AS sumScoreValue  
  37.     ->                   FROM sc_score  
  38.     ->                  GROUP BY studentid  
  39.     ->                  ORDER BY scoreValue DESC) tmp,  
  40.     ->                (SELECT @rank := 0,@groupid := '') m) a,  
  41.     ->        sc_student b  
  42.     ->  WHERE a.studentid = b.studentNo  
  43.     ->    AND a.rank = 1;  
  44. +-----------+-------------+---------------+  
  45. | studentid | studentName | sumScoreValue |  
  46. +-----------+-------------+---------------+  
  47. |         3 | cc          |           255 |  
  48. |         1 | aa          |           255 |  
  49. +-----------+-------------+---------------+  
  50. rows in set (0.00 sec)  
这是使用变量做的

自己重新做了


select s.id,s.stuid,stu.stuname, sumscore
from score s left join student stu on s.stuid = stu.stuid left join (select s.id,s.stuid,stu.stuname,sum(s.score) as sumscore
from score s left join student stu on s.stuid = stu.stuid where s.gradeid=4 and s.classid=1 and s.season=1 group by s.stuid)  as t1 on t1.id=s.id  where s.gradeid=4 and s.classid=1 and s.season=1  and sumscore in(select max(sumscore) from (select s.id,s.stuid,stu.stuname,sum(s.score) as sumscore
from score s left join student stu on s.stuid = stu.stuid where s.gradeid=4 and s.classid=1 and s.season=1 group by s.stuid) as t2)

0 0
原创粉丝点击