且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在mysql表中找到最大值

更新时间:2023-01-22 09:14:06

将您的数据库分成 3 个表,例如:

学生:

st_id |姓名 |电子邮件1 |约翰 |@a.com

课程:

cr_id |名称1 |数学2 |化学3 |生物4 |社会研究

学生课程:

st_id |cr_id |分数1 |1 |201 |2 |231 |3 |101 |4 |15

现在你可以:

SELECT s.name, MAX(sc.score) FROM Students s INNER JOIN StudentCourses sc ON s.st_id = sc.st_id;

i have a mysql table i.e

st_id | name | email | maths | chemistry | bio | social_study
1     | john |@a.com | 20    |  23       | 10  |  15


my question is how can i find the highest subject score, the second last and so on
Note that all the subject fields have int(11) values

Break your database into 3 tables like:

Students:

st_id | name | email  
1     | john |@a.com  

Courses:

cr_id | name  
1     | maths  
2     | chemistry  
3     | bio  
4     | social_studies

StudentCourses:

st_id | cr_id | score  
1     | 1     | 20   
1     | 2     | 23   
1     | 3     | 10   
1     | 4     | 15  

Now you can do:

SELECT s.name, MAX(sc.score) FROM Students s INNER JOIN StudentCourses sc ON s.st_id = sc.st_id;