引用自http://www.cnblogs.com/mo-beifeng/archive/2012/02/07/2341886.html#2341105
--按某一字段分组取最大(小)值所在行的数据
/*
数据如下:
name val memo
a    2   a2(a的第二个值)
a    1   a1--a的第一个值
a    3   a3:a的第三个值
b    1   b1--b的第一个值
b    3   b3:b的第三个值
b    2   b2b2b2b2
b    4   b4b4
b    5   b5b5b5b5b5

*/

--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a',    2,   'a2(a的第二个值)')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('b',    1,   'b1--b的第一个值')
insert into tb values('b',    3,   'b3:b的第三个值')
insert into tb values('b',    2,   'b2b2b2b2')
insert into tb values('b',    4,   'b4b4')
insert into tb values('b',    5,   'b5b5b5b5b5')

go

--一、按name分组取val最大的值所在行的数据。

 --方法1:

select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name       val         memo                
---------- ----------- --------------------
a          3           a3:a的第三个值
b          5           b5b5b5b5b5

*/

本人推荐使用1,3,4,结果显示1,3,4效率相同,2,5效率差些,不过我3,4效率相同毫无疑问,1就不一样了,想不搞了。

--二、按name分组取val最小的值所在行的数据。

--方法1:

select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name       val         memo                
---------- ----------- --------------------
a          1           a1--a的第一个值
b          1           b1--b的第一个值

*/

--三、按name分组取第一次出现的行所在的数据。

select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
/*
name       val         memo                
---------- ----------- --------------------
a          2           a2(a的第二个值)
b          1           b1--b的第一个值

*/

--四、按name分组随机取一条数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name

/*
name       val         memo                
---------- ----------- --------------------
a          1           a1--a的第一个值
b          5           b5b5b5b5b5

*/

--五、按name分组取最小的两个(N个)val

select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val

select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
/*
name       val         memo                
---------- ----------- --------------------
a          1           a1--a的第一个值
a          2           a2(a的第二个值)
b          1           b1--b的第一个值
b          2           b2b2b2b2

*/

--六、按name分组取最大的两个(N个)val

select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
/*
name       val         memo                
---------- ----------- --------------------
a          2           a2(a的第二个值)
a          3           a3:a的第三个值
b          4           b4b4
b          5           b5b5b5b5b5

*/

--七,假如整行数据有重复,所有的列都相同(例如下表中的第5,6两行数据完全相同)。

        按name分组取最大的两个(N个)val
 1  /*
 2 数据如下:
 3 name val memo
 4 a    2   a2(a的第二个值)
 5 a    1   a1--a的第一个值
 6 a    1   a1--a的第一个值
 7 a    3   a3:a的第三个值
 8 a    3   a3:a的第三个值
 9 b    1   b1--b的第一个值
10 b    3   b3:b的第三个值
11 b    2   b2b2b2b2
12 b    4   b4b4
13 b    5   b5b5b5b5b5
14 
15 */ 
16

附:mysql “group by ”与"order by"的研究--分类中最新的内容

这两天让一个数据查询难了。主要是对group by 理解的不够深入。才出现这样的情况

这种需求,我想很多人都遇到过。下面是我模拟我的内容表

我现在需要取出每个分类中最新的内容

select * from test group by category_id order by `date`

结果如下

明显。这不是我想要的数据,原因是msyql已经的执行顺序是

引用
写的顺序:select ... from... where.... group by... having... order by..
执行顺序:from... where...group by... having.... select ... order by...

所以在order by拿到的结果里已经是分组的完的最后结果。
由from到where的结果如下的内容。

到group by时就得到了根据category_id分出来的多个小组


到了select的时候,只从上面的每个组里取第一条信息结果会如下

即使order by也只是从上面的结果里进行排序。并不是每个分类的最新信息。
回到我的目的上 --分类中最新的信息
根据上面的分析,group by到select时只取到分组里的第一条信息。有两个解决方法
1,where+group by(对小组进行排序)
2,从form返回的数据下手脚(即用子查询)

由where+group by的解决方法
对group by里的小组进行排序的函数我只查到group_concat()可以进行排序,但group_concat的作用是将小组里的字段里的值进行串联起来。

select group_concat(id order by `date` desc) from `test` group by category_id


再改进一下

select * from `test` where id in(select SUBSTRING_INDEX(group_concat(id order by `date` desc),',',1) from `test` group by category_id ) order by `date` desc

子查询解决方案

select * from (select * from `test` order by `date` desc) `temp`  group by category_id order by `date` desc

mysql中 groupby分组的更多相关文章

  1. mysql中的分组统计函数及其用法实例

    1.使用group by对数据进行分组:select 字段名... from tablename group by 字段名...:可以把分组.排序.统计等等都结合在一起使用,实际应用中也多是这样的: ...

  2. 如何在MySQL中查询每个分组的前几名【转】

    问题 在工作中常会遇到将数据分组排序的问题,如在考试成绩中,找出每个班级的前五名等. 在orcale等数据库中可以使用partition语句来解决,但在mysql中就比较麻烦了.这次翻译的文章就是专门 ...

  3. 在mysql中使用group by和order by取每个分组中日期最大一行数据

    转载自:https://blog.csdn.net/shiyong1949/article/details/78482737 在mysql中使用group by进行分组后取某一列的最大值,我们可以直接 ...

  4. 如何在mysql中查询每个分组的前几名

    问题 在工作中常会遇到将数据分组排序的问题,如在考试成绩中,找出每个班级的前五名等.  在orcale等数据库中可以使用partition 语句来解决,但在MySQL中就比较麻烦了.这次翻译的文章就是 ...

  5. 一个MySQL中两表联合update的例子(并带有group by分组)

    内容简介 本文主要展示了在MySQL中,使用两表联合的方式来更新其中一个表字段值的SQL语句. 也就是update table1 join table2 on table1.col_name1=tab ...

  6. Python中itertools.groupby分组的使用

    Python中itertools.groupby分组的使用 有时候我们需要给一个列表按照某个属性分组,可以借助groupby来实现. 比如:一下列表我想以严重程度给它分组,并求出每组的元素个数. fr ...

  7. MySQL中group_concat函数 --- 很有用的一个用来查询出所有group by 分组后所有 同组内的 内容

    本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) . MySQL中group_concat函数 完整的语法如下: grou ...

  8. mysql中explain

    1.select_type: /* select_type 使用 SIMPLE */explain select * from tb_shop_order where id='201603292570 ...

  9. laravel groupby分组问题。

    laravel 5.7使用groupBy分组查询时会提示一个错误,但是sql可以执行. 因为:mysql从5.7以后,默认开启了严格模式. 解决方法:将/config/database.php 中:关 ...

随机推荐

  1. 06二叉树、Map、Collections、适配器

    06二叉树.Map.Collections.适配器-2018/07/16 1.set集合,无索引,不可以重复,无序(存取不一致) 2.TreeSet用来对象元素进行排序,可以保证元素唯一 储存自定义对 ...

  2. UVA - 12661 Funny Car Racing (Dijkstra算法)

    题目: 思路: 把时间当做距离利用Dijkstra算法来做这个题. 前提:该结点e.c<=e.a,k = d[v]%(e.a+e.b); 当车在这个点的1处时,如果在第一个a这段时间内能够通过且 ...

  3. Re0:DP学习之路 Proud Merchants HDU - 3466

    解法 排序+01背包 这里的排序规则用q-p升序排列这里是一个感觉是一个贪心的策略,为什么这样做目前也无法有效的证明或者说出来 然后就是01背包加了一个体积必须大于什么值可以装那么加一个max(p,q ...

  4. Python学习-字符串的基本知识

    字符串的基本知识 根据所展示形式的不同,字符串也可以分为两类 原始字符串: 使用单引号包括:‘liuwen’ 使用双引号包括:"liuwen" 使用3个单引号包括 :'''liuw ...

  5. 这几道Java集合框架面试题在面试中几乎必问

    Arraylist 与 LinkedList 异同 1. 是否保证线程安全: ArrayList 和 LinkedList 都是不同步的,也就是不保证线程安全: 2. 底层数据结构: Arraylis ...

  6. ubuntu 通过ppa源安装mysql5.6

    添加mysql5.6的源 sudo add-apt-repository -y ppa:ondrej/mysql-5.6 更新源 sudo apt-get update 安装mysql5.6 sudo ...

  7. gitlab root 账号 忘记密码如何重置

    shell>cd /home/git/gitlabshell> su gitshell>bundle exec rails console productionirb(main):0 ...

  8. git 安装 使用

    git 安装--------------------------------------yum install git -y git 下载项目----------------------------- ...

  9. 热词解析(9) — hangry

    今天给大家介绍一个非常有趣.又超级实用的词!!中文叫"饿极而怒",英文叫... 不知道你有没有这样的经历,当你饿着肚子等着你妈做饭,结果你妈却在麻将桌上不下来,你就越来越饿,越饿越 ...

  10. array copy rotate in Pointwise

    goal: # # Copyright 2010 (c) Pointwise, Inc. # All rights reserved. # # This sample Pointwise script ...