日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

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

發(fā)布時(shí)間:2025/3/11 数据库 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql max嵌套select_使用嵌套select子式 解决mysql不能叠加使用如max(sum())的问题 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

網(wǎng)上也有解決方案 有的有瑕疵 有的較復(fù)雜(mysql沒(méi)有分析函數(shù),可以使用變量實(shí)現(xiàn)) select sumScoreValue,studentid,studentName from sc_studentb, ( select sum (scoreValue) as sumScoreValue,studentid from sc_score group by studentid order by sumSc

網(wǎng)上也有解決方案 有的有瑕疵 有的較復(fù)雜(mysql沒(méi)有分析函數(shù),可以使用變量實(shí)現(xiàn))select sumScoreValue,studentid,studentName from sc_student b,

(select sum(scoreValue) as sumScoreValue, studentid

from sc_score group by studentid

order by sumScoreValue desc limit 1) as a

where a.studentid=b.studentNo

//這樣做 只能查詢第一名只有一個(gè)的情況 很巧妙

mysql> select studentid,scoreValue from sc_score;

+-----------+------------+

| studentid | scoreValue |

+-----------+------------+

| 1 | 80 |

| 1 | 85 |

| 1 | 90 |

| 2 | 75 |

| 2 | 80 |

| 2 | 84 |

| 3 | 85 |

| 3 | 85 |

| 3 | 85 |

+-----------+------------+

9 rows in set (0.00 sec)

mysql> SELECT studentNo,studentName FROM sc_student;

+-----------+-------------+

| studentNo | studentName |

+-----------+-------------+

| 1 | aa |

| 2 | bb |

| 3 | cc |

+-----------+-------------+

3 rows in set (0.00 sec)

mysql> SELECT a.studentid,

-> b.studentName,

-> a.sumScoreValue

-> FROM (SELECT tmp.studentid,

-> tmp.sumScoreValue,

-> IF(@groupid = tmp.sumScoreValue,@rank := 1,@rank := @rank + 1) AS rank,

-> @groupid := tmp.sumScoreValue

-> FROM (SELECT studentid,

-> SUM(scoreValue) AS sumScoreValue

-> FROM sc_score

-> GROUP BY studentid

-> ORDER BY scoreValue DESC) tmp,

-> (SELECT @rank := 0,@groupid := '') m) a,

-> sc_student b

-> WHERE a.studentid = b.studentNo

-> AND a.rank = 1;

+-----------+-------------+---------------+

| studentid | studentName | sumScoreValue |

+-----------+-------------+---------------+

| 3 | cc | 255 |

| 1 | aa | 255 |

+-----------+-------------+---------------+

2 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)

總結(jié)

以上是生活随笔為你收集整理的mysql max嵌套select_使用嵌套select子式 解决mysql不能叠加使用如max(sum())的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。