mysql group 陷阱
2015-12-28 19:24
567 查看
mysql> SELECT -> sn,Clientsn,createdtime -> FROM -> ClientCashDetail -> WHERE -> createdTime < '2015-12-25 00:00:00' -> GROUP BY clientSn -> ORDER BY createdtime DESC; +-------+----------+---------------------+ | sn | Clientsn | createdtime | +-------+----------+---------------------+ | 20146 | 12904 | 2015-12-28 18:51:59 | | 20143 | 501 | 2015-12-28 17:36:21 | | 20141 | 1177 | 2015-12-28 17:12:55 | | 20135 | 57 | 2015-12-28 16:47:43 | | 20124 | 1407 | 2015-12-28 16:19:15 | | 20108 | 12808 | 2015-12-28 16:05:52 | | 20107 | 592 | 2015-12-28 16:05:33 | | 20102 | 12738 | 2015-12-28 16:03:21 | | 20094 | 1570 | 2015-12-28 15:59:33 | | 20091 | 2102 | 2015-12-28 15:57:18 | | 20086 | 1659 | 2015-12-28 15:50:31 | | 20084 | 13006 | 2015-12-28 15:07:03 | | 20083 | 512 | 2015-12-28 15:04:51 | mysql> select Clientsn ,count(*) from ClientCashDetail group by Clientsn order by count(*) ; | 12 | 100 | | 392 | 100 | | 253 | 105 | | 605 | 105 | | 55 | 107 | | 22 | 109 | | 592 | 110 | | 65 | 110 | | 31 | 112 | | 71 | 113 | | 57 | 118 | | 127 | 123 | | 1 | 127 | | 45 | 140 | | 351 | 156 | | 15 | 160 | | 13 | 187 | | 1123 | 198 | +----------+----------+ 1075 rows in set (0.00 sec) mysql> select sn,Clientsn,createdtime from ClientCashDetail where createdTime >= '2015-12-24 00:00:00' and Clientsn=1123; +-------+----------+---------------------+ | sn | Clientsn | createdtime | +-------+----------+---------------------+ | 19951 | 1123 | 2015-12-24 14:36:20 | | 19995 | 1123 | 2015-12-24 15:11:59 | | 19996 | 1123 | 2015-12-24 15:19:45 | | 20118 | 1123 | 2015-12-28 16:09:24 | +-------+----------+---------------------+ 4 rows in set (0.00 sec) mysql> select sn,Clientsn,createdtime from ClientCashDetail where createdTime >= '2015-12-24 00:00:00' and Clientsn=1123 group by Clientsn; +-------+----------+---------------------+ | sn | Clientsn | createdtime | +-------+----------+---------------------+ | 19951 | 1123 | 2015-12-24 14:36:20 | +-------+----------+---------------------+ 1 row in set (0.00 sec) mysql> select sn,Clientsn,createdtime from ClientCashDetail where createdTime >= '2015-12-24 00:00:00' and Clientsn=1123 group by Clientsn order by createdtime desc; +-------+----------+---------------------+ | sn | Clientsn | createdtime | +-------+----------+---------------------+ | 19951 | 1123 | 2015-12-24 14:36:20 | +-------+----------+---------------------+ 1 row in set (0.00 sec) ------------------------------------------------------------------------------------------------------------------------------------------------------------------- mysql> select sn,Clientsn,createdtime from ClientCashDetail where createdTime >= '2015-12-24 00:00:00' and Clientsn=1123 ; +-------+----------+---------------------+ | sn | Clientsn | createdtime | +-------+----------+---------------------+ | 19951 | 1123 | 2015-12-24 14:36:20 | | 19995 | 1123 | 2015-12-24 15:11:59 | | 19996 | 1123 | 2015-12-24 15:19:45 | | 20118 | 1123 | 2015-12-28 16:09:24 | +-------+----------+---------------------+ 4 rows in set (0.00 sec) mysql> select max(sn),Clientsn,max(createdtime) from ClientCashDetail where createdTime >= '2015-12-24 00:00:00' and Clientsn=1123 group by Clientsn ; +---------+----------+---------------------+ | max(sn) | Clientsn | max(createdtime) | +---------+----------+---------------------+ | 20118 | 1123 | 2015-12-28 16:09:24 | +---------+----------+---------------------+ 1 row in set (0.00 sec) | 12825 | 2015-12-24 12:05:53 | | 12861 | 2015-12-24 11:49:03 | | 12899 | 2015-12-24 09:12:15 | | 12904 | 2015-12-28 19:00:04 | | 12914 | 2015-12-25 13:53:19 | | 12941 | 2015-12-26 01:57:16 | | 13004 | 2015-12-28 10:08:04 | | 13006 | 2015-12-28 16:06:18 | +----------+---------------------+ 1075 rows in set (0.00 sec) mysql> select ClientSn, max(createdtime) from ClientCashDetail group by ClientSn; select SUM(accountBalance) from ( SELECT a.* FROM ClientCashDetail a INNER JOIN (SELECT MAX(createdtime) AS mtime FROM ClientCashDetail where createdTime < '2015-12-25 00:00:00' GROUP BY ClientSn) b ON a.createdtime = b.mtime) c
相关文章推荐
- mysql user权限表
- mysql 的 find_in_set,INSTR函数使用方法
- 连接MySQL数据库时常见故障问题的分析与解决
- JDBC_获取插入记录的主键值(基于MySQL数据库)
- Mysql分页查询获取totalCount大幅提升性能的办法总结
- MySQL对数据表进行分组查询
- mysql的查询、子查询及连接查询
- mysql分布式中间件cobar
- mysql中对数字处理的函数
- MySQL和SQLServer的比较
- Mysql优化器源码
- 得前一次MySQL操作所影响的记录行数
- 关于MySQL分页几个好的博文
- kettle:mongdb与mysql互传数据
- 错误 175: 具有固定名称“MySql.Data.MySqlClient”的 ADO.NET 提供程序未在计算
- mysql alter
- Mysql 查询缓存
- mysql将时间戳直接转换成日期时间
- MySQL数据库引擎介绍、区别、创建和性能测试的深入分析
- 【mysql】添加对emoji的支持