使用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,子查询里只扫描了一跳记录。

很明显。在这种情况下第二个比第一个效率高点。

MAX函数和GROUP BY 语句一起使用的一个误区的更多相关文章

  1. MySQL中MAX函数与Group By一起使用的注意事项(转)

    mysql> select * from test; +----+-------+------+-------+ | id | name | age | class | +----+------ ...

  2. GROUP BY语句与HAVING语句的使用

    一.GROUP BY GROUP BY语句用来与聚合函数(aggregate functions such as COUNT, SUM, AVG, MIN, or MAX.)联合使用来得到一个或多个列 ...

  3. SQL中GROUP BY语句与HAVING语句的使用

    GROUP BY语句,经过研究和练习,终于明白如何使用了,在此记录一下同时添加了一个自己举的小例子,通过写这篇文章来加深下自己学习的效果,还能和大家分享下,同时也方便以后查阅,一举多得 一.GROUP ...

  4. 【转】SQL中GROUP BY语句与HAVING语句的使用

    一.GROUP BY GROUP BY语句用来与聚合函数(aggregate functions such as COUNT, SUM, AVG, MIN, or MAX.)联合使用来得到一个或多个列 ...

  5. group by语句,聚合函数与其他语句(关系,执行过程)

    (本文一切基于SQL SERVER 2008 R2) 先来看一个语句执行过程发生的错误提示: 消息 8120,级别 16,状态 1,第 2 行 选择列表中的列 'SC.CNO' 无效,因为该列没有包含 ...

  6. 深入理解group by 语句的执行顺序 from→where→group by→select(含聚合函数)

    由于之前没有对group by 语句的执行顺序(执行原理)做深入的了解,所以导致在实际应用过程中出现了一些问题.举个简单的粟子,比如一个表testA中的所有数据如下图: 我现在想从testA中查询us ...

  7. [数据库]关于MAX()函数的一个坑

    之前写了这么一条sql语句,目的是取出表中itemid最大的那一条数据. SELECT date, MAX(itemid) AS itemid, group FROM mytable GROUP BY ...

  8. mysql之聚合函数、group by、having

    sql中提供聚合函数可以用来统计,求和,求最值等 那么聚合函数有哪些呢? COUNT    统计行数量 SUM         求某一列的和 AVG          求某一列的平均值 MAX  求某 ...

  9. MySQL GROUP BY语句

    GROUP BY 语句根据一个或多个列对结果集进行分组 在分组的列上我们可以使用COUNT.SUM.AVG等函数 SELECT column_name,function(column_name) FR ...

随机推荐

  1. How Does #DeepDream Work?

    How Does #DeepDream Work? Do neural networks hallucinate of electronic dogs? If you’ve been browsing ...

  2. Ubuntu下与菜单和图标相关的几个文件夹

    转自UBUNTU下与菜单和图标相关的几个文件夹 /usr/share/icons  系统图标文件夹 /usr/share/applications  系统菜单文件夹,要在左上角的应用程序菜单中添加一项 ...

  3. CF Rook, Bishop and King

    http://codeforces.com/contest/370/problem/A 题意:车是走直线的,可以走任意多个格子,象是走对角线的,也可以走任意多个格子,而国王可以走直线也可以走对角线,但 ...

  4. Delphi GDI+基本用法总结

    GDI+以前只是听说过,还没怎么用过,这段时间用了用,觉得挺好用的.在这里总结一下.留个备忘. GDI+(Graphics Device Interface plus)是Windows XP中的一个子 ...

  5. Case Studies: Retail and Investment Banks Use of Social Media

    The past couple of months have seen an increased acknowledgement of the role social media has to pla ...

  6. 函数xdes_find_bit

    使用方法 free = xdes_find_bit(descr, XDES_FREE_BIT, TRUE,hint % FSP_EXTENT_SIZE, mtr); /**************** ...

  7. POJ_3258_River_Hopscotch_[NOIP2015]_(二分,最大化最小值)

    描述 http://poj.org/problem?id=3258 给出起点和终点之间的距离L,中间有n个石子,给出第i个石子与起点之间的距离d[i],现在要去掉m个石子(不包括起终点),求距离最近的 ...

  8. Xcode7创建纯代码空白工程

    0: Create a new project  with  'single view controller' A: Xcode Settings 1: migrate launch image B: ...

  9. ArcGIS for Android示例解析之空间查询-----QueryTask

    转自:http://blog.csdn.net/wozaifeiyang0/article/details/7331450 QueryTask 查询功能在GIS中是一个不可或缺的重要功能,示例中提供了 ...

  10. HTTP分段下载

    现代WEB服务器都支持大文件分段下载,加快下载速度,判断WEB服务器是否支持分段下载通过返回头是否有 Accept-Ranges: bytes 字段.分段下载分为两种,一种就是一次请求一个分段,一种就 ...