zoukankan      html  css  js  c++  java
  • mysql的group by查询

    下面是多种写法,针对使用group by后得到最新记录的测试及结果:

    说明:我在测试的时候,因为我的表数据在增加,得到最新的数据可能不同

     1 -- 1、得到每个分组中id最小的那条记录
     2 select * from pic where point_code in('XX00024', 'XX00016', 'XX00025') and addtime > '2018-09-03'
     3 group by point_code
     4 -- 37033746    XX00016    长江大桥路口    http://xxx/10.58.237.73_01_20180903001241103.jpg    2018-09-03 00:12:41
     5 -- 37033631    XX00024    沱六桥路口    http://xxx/10.25.77.3_01_20180903000355528.jpg    2018-09-03 00:03:55
     6 -- 37033485    XX00025    高新区立交桥    http://xxx/10.210.98.143_01_20180903000211230.jpg    2018-09-03 00:02:11
     7 
     8 -- 2、意图,先通过子查询,按照ID降序,得到每个分组中最新的一条记录
     9 select * from 
    10 (
    11     select * from pic where point_code in('XX00024', 'XX00016', 'XX00025') and addtime > '2018-09-03' order by id desc -- limit 1000
    12 ) tt
    13 group by point_code;
    14 -- 结果还是每个分组最小的记录,与第一种写法结果相同
    15 
    16 -- 3、在第二种写法的子查询周添加了一个limit,limit后面的数字大于你查询的总条数即可
    17 select * from 
    18 (
    19     select * from pic where point_code in('XX00024', 'XX00016', 'XX00025') and addtime > '2018-09-03' order by id desc limit 10000000000
    20 ) tt
    21 group by point_code;
    22 -- 结果返回每个分组中最新的那条记录
    23 -- 37064239    XX00016    长江大桥路口    http://xxx/10.58.237.73_01_20180903144242037.jpg    2018-09-03 14:42:42
    24 -- 37064240    XX00024    沱六桥路口    http://xxx/10.25.77.3_01_20180903143857383.jpg    2018-09-03 14:38:57
    25 -- 37064139    XX00025    高新区立交桥    http://xxx/10.210.98.143_01_20180903143713651.jpg    2018-09-03 14:37:13
    26 
    27 
    28 -- 4、在第一中写法的group by后面添加一个desc,如果不加条件进行过滤,则查询时间会很长
    29 select * from pic where point_code in('XX00024', 'XX00016', 'XX00025') and addtime > '2018-09-03'
    30 group by point_code desc
    31 -- 返回结果是每个分组的最新记录
    32 -- 37064139    XX00025    高新区立交桥    http://xxx/10.210.98.143_01_20180903143713651.jpg    2018-09-03 14:37:13
    33 -- 37064240    XX00024    沱六桥路口    http://xxx/10.25.77.3_01_20180903143857383.jpg    2018-09-03 14:38:57
    34 -- 37064239    XX00016    长江大桥路口    http://xxx/10.58.237.73_01_20180903144242037.jpg    2018-09-03 14:42:42
    35 
    36 -- 5、一般我们在查询结果都会按照一定规则进行排序,对第四种写法进行排序
    37 select * from pic where point_code in('XX00024', 'XX00016', 'XX00025') and addtime > '2018-09-03'
    38 group by point_code desc order by point_code
    39 -- 返回结果又变成了每个分组的最小记录了
    40 -- 37033746    XX00016    长江大桥路口    http://xxx/10.58.237.73_01_20180903001241103.jpg    2018-09-03 00:12:41
    41 -- 37033631    XX00024    沱六桥路口    http://xxx/10.25.77.3_01_20180903000355528.jpg    2018-09-03 00:03:55
    42 -- 37033485    XX00025    高新区立交桥    http://xxx/10.210.98.143_01_20180903000211230.jpg    2018-09-03 00:02:11
    43 
    44 -- 6、将第五种写法的排序,放到分组外边
    45 select * from 
    46 (
    47     select * from pic where point_code in('XX00024', 'XX00016', 'XX00025') and addtime > '2018-09-03'
    48     group by point_code desc 
    49 ) t order by point_code
    50 -- 返回的结果又是每个分组的最新值
    51 -- 37064239    XX00016    长江大桥路口    http://xxx/10.58.237.73_01_20180903144242037.jpg    2018-09-03 14:42:42
    52 -- 37064240    XX00024    沱六桥路口    http://xxx/10.25.77.3_01_20180903143857383.jpg    2018-09-03 14:38:57
    53 -- 37064139    XX00025    高新区立交桥    http://xxx/10.210.98.143_01_20180903143713651.jpg    2018-09-03 14:37:13

    在使用group by希望得到最新的数据时,如果想采用子查询先行排序,注意需要添加limit才会生效,这是我的测试结果

    网上百度了一些,许多博客,没有写limit好像也生效了,不知道是不是mysql不同的版本问题。

    好像只能针对单表进行这样操作,表连接好像就没有效果了

  • 相关阅读:
    【编程基础】const与#define的区别
    【Unity3D】模仿制作“神庙逃亡”吃金币后金币飞出屏幕效果
    【基础数学】素数判定、素数打表
    【NYOJ-187】快速查找素数—— 枚举法、筛选法、打表法
    【基础数学】质数,约数,分解质因数,GCD,LCM
    【NYOJ-35】表达式求值——简单栈练习
    【UVa-679】小球下落——二叉树的编号
    【UVa-442】矩阵链乘——简单栈练习
    【UVa-514】铁轨——栈的学习
    gitignore git提交忽略文件
  • 原文地址:https://www.cnblogs.com/TheoryDance/p/9578601.html
Copyright © 2011-2022 走看看