忘记密码   免费注册 查看新帖 | 论坛精华区

ChinaUnix.net

  平台 论坛 博客 认证专区 大话IT HPC论坛 徽章 文库 沙龙 自测 下载 频道自动化运维 虚拟化 储存备份 C/C++ PHP MySQL 嵌入式 Linux系统
12
最近访问板块 发新帖
楼主: yuhuohu

求效率最高的mysql语句 [复制链接]

论坛徽章:
0
发表于 2012-05-04 22:34 |显示全部楼层
回复 2# junsansi


如果是MYSQL,有点问题吧。
   

论坛徽章:
0
发表于 2012-05-04 22:36 |显示全部楼层
本帖最后由 alang85 于 2012-05-04 22:39 编辑

回复 1# yuhuohu


正确的写法如下:
SELECT MAX(a.sum_money), a.date_time
  FROM (SELECT SUM(money) sum_money,
               DATE(datetimes) date_time,
               HOUR(datetimes) hour_time
          FROM t1
         GROUP BY DATE(datetimes), HOUR(datetimes)) a
GROUP BY a.date_time;

附带表结构:
CREATE TABLE `t1` (
  `id` int(11) NOT NULL,
  `employees_name` varchar(20) DEFAULT NULL,
  `datetimes` datetime,
  `money` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
测试数据语句如下:
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('1','甲','2012-04-10 10:10:10','1');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('2','乙','2012-04-10 10:10:10','2');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('3','丙','2012-04-10 10:10:10','3');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('4','丁','2012-04-10 10:10:10','4');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('5','甲','2012-04-10 11:10:10','2');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('6','乙','2012-04-10 11:10:10','3');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('7','丙','2012-04-10 11:10:10','3');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('8','丁','2012-04-10 11:10:10','5');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('9','甲','2012-04-11 10:10:10','3');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('10','乙','2012-04-11 10:10:10','6');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('11','丙','2012-04-11 10:10:10','5');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('12','丁','2012-04-11 10:10:10','8');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('13','甲','2012-04-11 11:10:10','2');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('14','乙','2012-04-11 11:10:10','3');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('15','丙','2012-04-11 11:10:10','3');
insert into `t1` (`id`, `employees_name`, `datetimes`, `money`) values('16','丁','2012-04-11 11:10:10','5');

论坛徽章:
0
发表于 2017-04-04 13:04 |显示全部楼层
发现自己看不懂了,退化严重
来自安卓客户端来自客户端
您需要登录后才可以回帖 登录 | 注册

本版积分规则

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号 北京市公安局海淀分局网监中心备案编号:11010802020122
广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员  联系我们:
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP