标题:SQL如何查询出最偏科的学生信息
只看楼主
ithaibo
Rank: 2
等 级:论坛游民
帖 子:52
专家分:50
注 册:2012-3-30
结帖率:50%
 问题点数:0 回复次数:5 
SQL如何查询出最偏科的学生信息
如题,一张表:
+----+--------+---------+------+------------+--------------+---------+
| id | name   | english | math | birthday   | native_place | chinese |
+----+--------+---------+------+------------+--------------+---------+
|  1 | 潘怡茹 |      86 |   91 | 1990-01-01 | 上海         |      97 |
|  2 | 刘濮松 |      88 |   68 | 1990-02-01 | 上海         |      96 |
|  3 | 刘吉如 |      85 |   53 | 1990-03-01 | 上海         |      70 |
|  4 | 李岩珂 |      85 |   70 | 1990-04-01 | 上海         |      96 |
|  5 | 王晓博 |      85 |   79 | 1990-05-01 | 上海         |      46 |
|  6 | 李帅旭 |      79 |   76 | 1990-06-01 | 上海         |      97 |
|  7 | 李静瑶 |      89 |   61 | 1990-07-01 | 上海         |      92 |
|  8 | 金纾凡 |      80 |   43 | 1990-08-01 | 上海         |      83 |
|  9 | 秦梓航 |      57 |   46 | 1990-09-01 | 上海         |      86 |
| 10 | 关颖利 |      80 |   77 | 1991-01-01 | 上海         |      84 |
+----+--------+---------+------+------------+--------------+---------+

问题:如何找出最偏科的学生姓名?
搜索更多相关主题的帖子: english 上海 如何 信息 
2015-04-30 21:13
ithaibo
Rank: 2
等 级:论坛游民
帖 子:52
专家分:50
注 册:2012-3-30
得分:0 
select name from student
    where
        abs(chinese-math) in (select max(tmp.score)
                                from (
                                          select max(abs(s1.chinese-s1.math)) score from student s1
                                    union select max(abs(s2.chinese-s2.english)) score from student s2
                                    union select max(abs(s3.english-s3.math)) score from student s3
                                    ) tmp)
    or   
        abs(chinese-english)in (select max(tmp1.score)
                                from (
                                          select max(abs(s1.chinese-s1.math)) score from student s1
                                    union select max(abs(s2.chinese-s2.english)) score from student s2
                                    union select max(abs(s3.english-s3.math)) score from student s3
                                    ) tmp1)
    or   
        abs(english-math) in (select max(tmp2.score)
                                from (
                                          select max(abs(s1.chinese-s1.math)) score from student s1
                                    union select max(abs(s2.chinese-s2.english)) score from student s2
                                    union select max(abs(s3.english-s3.math)) score from student s3
                                    ) tmp2);
2015-05-02 16:40
ithaibo
Rank: 2
等 级:论坛游民
帖 子:52
专家分:50
注 册:2012-3-30
得分:0 
结果为:
+--------+
| name   |
+--------+
| 金纾凡 |
| 秦梓航 |
+--------+
2015-05-02 16:40
ithaibo
Rank: 2
等 级:论坛游民
帖 子:52
专家分:50
注 册:2012-3-30
得分:0 
如有优化方案,请指教!
2015-05-02 16:41
donaldlo
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-8-19
得分:0 
如果只是要找出任意两科差异最大的学生
select * from (
select id,name,max(math,english,cheese)-min(math,english,cheese) score1 from score)
order by score1 descending
2015-08-19 12:12
诸葛欧阳
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:流年
等 级:贵宾
威 望:82
帖 子:2790
专家分:14619
注 册:2014-10-16
得分:0 
楼上更简洁

一片落叶掉进了回忆的流年。
2015-10-16 09:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-444696-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.172154 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved