1 --1、得到每个分组中id最小的那条记录
2 select * from pic where point_code in('XX00024', 'XX00016', 'XX00025') and addtime > '2018-09-03'
3 group bypoint_code4 --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 ) tt13 group bypoint_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 ) tt21 group bypoint_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 bypoint_code39 --返回结果又变成了每个分组的最小记录了
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 bypoint_code50 --返回的结果又是每个分组的最新值
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