php - 如何在mysql表中找到最大值

标签 php mysql

我有一个 mysql 表,即

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


我的问题是如何找到最高的科目分数,倒数第二等等
请注意,所有主题字段都有 int(11) 值

最佳答案

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

学生们:

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

类(class):

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

学生类(class):

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

现在您可以:

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

关于php - 如何在mysql表中找到最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5482067/

相关文章:

php - 多字值问题汇总

php - Eloquent 查询多重关系深层

mysql - 选择不在间隔内的日期

mysql - Insert语句与外键约束冲突

mysql - Laravel 表拒绝在迁移表上注册并回滚或迁移

javascript - 暂停和重新启动按钮不起作用

php - 通过在 codeigniter 中加载 View 来打开 Bootstrap 模式对话框

Php mail() 与雅虎 : Can someone Simply Explain Steps required for YAHOO to receive mail from php mail function?

php 从对象数组中删除对象

mysql json where子句