摘自yueliangdao0608.cublog.cn
http://blog.chinaunix.net/u/29134/
MAX函数和GROUP BY 语句一起使用的一个误区
使用MAX 函数和 GROUP 的时候会有不可预料的数据被SELECT 出来。
下面举个简单的例子:
想知道每个SCOREID 的 数学成绩最高的分数。
表信息:
/*DDL Information For - test.lkscore*/
--------------------------------------
Table Create Table
------- -----------------------------------------------------------------------------
lkscore CREATE TABLE `lkscore` (
`scoreid` int(11) DEFAULT NULL,
`chinese` int(11) DEFAULT '0',
`math` int(11) DEFAULT '0',
KEY `fk_class` (`scoreid`),
CONSTRAINT `fk_class` FOREIGN KEY (`scoreid`) REFERENCES `lkclass` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312
select * from lkscore;
query result(12 records)
scoreid chinese math
1 90 80
2 100 99
3 29 98
4 87 79
5 89 99
1 49 98
3 98 56
2 76 88
2 80 90
3 90 70
1 90 90
1 67 90
错误的SELECT
select scoreid,chinese,max(math) max_math from lkscore group by scoreid;
query result(5 records)
scoreid chinese max_math
1 90 98
2 100 99
3 29 98
4 87 79
5 89 99
上面的90明显不对。
方法一:
select scoreid,chinese,math max_math from
(
select * from lkscore order by math desc
) T
group by scoreid;
query result(5 records)
scoreid chinese max_math
1 49 98
2 100 99
3 29 98
4 87 79
5 89 99
方法二:
select * from lkscore a where a.math = (select max(math) from lkscore where scoreid = a.scoreid) order by scoreid asc;
query result(5 records)
scoreid chinese max_math
1 49 98
2 100 99
3 29 98
4 87 79
5 89 99
这个也是用MAX函数,而且还用到了相关子查询。
我们来看一下这两个的效率如何:
explain
select scoreid,chinese,math max_math from (select * from lkscore order by math desc) T group by scoreid;
query result(2 records)
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL (NULL) (NULL) (NULL) (NULL) 12 Using temporary; Using filesort
2 DERIVED lkscore ALL (NULL) (NULL) (NULL) (NULL) 12 Using filesort
很明显,有两个FULL TABLE SCAN。
explain
select scoreid,chinese,math max_math from lkscore a where a.math =
(select max(math) from lkscore where scoreid = a.scoreid) order by scoreid asc;
query result(2 records)
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY a index (NULL) fk_class 5 (NULL) 12 Using where
2 DEPENDENT SUBQUERY lkscore ref fk_class fk_class 5 a.scoreid 1 Using where
第二个就用了KEY,子查询里只扫描了一跳记录。
很明显。在这种情况下第二个比第一个效率高点。
分享到:
相关推荐
linux内核中的min、max函数1
# 也可以对列表和元组使用 max([1,2]) max((1,2)) # 还可以指定comparator function max('ah', 'bf', key=lambda x: x[1]) def comparator(x): return x[1] max('ah', 'bf', key=comparator) 希望本文所述对大家的...
TIA博途中如何使用MAX和MIN数学函数求取几个变量的最大值和最小值?
max函数求4个数最大值
对于lena图像的量化,以及利用Lloyd Max函数量化,有文档,有代码,运行通过的,平台式matlab!
max函数.xls
(2)编写一个函数把华氏温度转换为摄氏温度,转换公式为C= (F一32)*5/9。 (3)编写重载函数Maxl可分别求取两个整数,三个整数,两个双精度数,三个双精度数的最大值。 (4)使用重载函数模板重新实现上小题中的...
excel max函数如何使用.docx
在select 语句中可以使用group by 子句将行划分成较小的组,一旦使用分组后select操作的对象变为各个分组后的数据,使用聚组函数返回的是每一个组的汇总信息。 使用having子句 限制返回的结果集。group by 子句可以...
1、本文详细描述了C++语言max函数的介绍及使用方法。 2、通过详细示例,让读者更直观地阅读,更清晰的理解。 3、示例代码可直接复制,编译后可直接运行。 4、根据示例以及运行结果,让读者加强记忆及理解。
编写自定义函数max,用来求三个数的最大值(要求:数据的输入、输出在主函数中实现)。
辅以控制软件和特别的外围电路设计,就能实现一个低成本、多功能、高精度、输出频率连续可调的频率合成式波形发生器。并对其振荡频率控制、信号输出幅度控制、频率和幅度的控制作了比较详细的论述。最后根据MAX038的...
主要介绍了MySQL中无GROUP BY情况下直接使用HAVING语句的问题探究,同时探究了该情况下MAX与MIN功能的使用情况,需要的朋友可以参考下
c++ max函数
mysql 中order by 与...要查出group by中最大的或最小的某一字段使用 max或min函数。例:select sum(click_num) as totalnum,max(update_time) as update_time,count(*) as totalarticle from article_detail where
在介绍GROUP BY 和 HAVING 子句前,我们必需先讲讲sql语言中一种特殊的函数:聚合函数, 例如SUM, COUNT, MAX, AVG等。...通过使用GROUP BY 子句,可以让SUM 和 COUNT 这些函数对属于一组的数据起作用。
利用Excel MAX函数或SUBTOTAL函数取得各科成绩的最高分.rar,有一份期末考试成绩表,要求计算出各科成绩的最高分,使用MAX函数或SUBTOTAL函数可以实现之。
定义一个数getMax()函数,返回三个数(从键盘输入的整数)中最大值。 def getMax(): num1=int(input("请在键盘上输入第1个整数:")) num2=int(input("请在键盘上输入第2个整数:")) num3=int(input("请在键盘上...
利用python进行求解,求解的要求是不能使用python内部封装好的函数例如:max way1: def findmax(data,n): if n==1: return data[0] else: maxi=data[0] for i in data[1:]: if maxi<i: maxi=i return ...
本文实例讲述了JavaScript使用Max函数返回两个数字中较大数的方法。分享给大家供大家参考。具体如下: JavaScript的Math对象带有一个max函数用于获取两个数字的较大数,下面的代码详细演示了max的用法 <!DOCTYPE...