| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2968 人关注过本帖
标题: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
快速回复:SQL如何查询出最偏科的学生信息
数据加载中...
 
   



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

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