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 语句一起使用的一个误区http://yueliangdao0608.blog.51cto.com/397025/81278

相关文章:

  • 2021-09-14
  • 2021-11-10
  • 2022-12-23
  • 2022-02-10
  • 2021-09-25
  • 2021-09-04
  • 2021-10-22
猜你喜欢
  • 2022-12-23
  • 2021-07-02
  • 2021-08-01
  • 2021-08-15
相关资源
相似解决方案