MYSQL 分组并查询最大时间

1.利用子查询查询最大时间

2.在外层比较时间相等

注意点:max只对当列有效


##
select ta.int_interpreting_data_id,ta.str_value,ta.dt_report_date,ta.dataId,ta.db_name,ta.int_interpreting_data_id,ta.str_property_id 
from  t_interpreting_data ta,(
select max(dt_report_date) drt
from t_interpreting_data 
where str_property_id='Windows Server 2003_244'
group by db_name,str_norm_number
order by dt_report_date desc 

) tid
where ta.str_property_id='Windows Server 2003_244' and ta.dt_report_date = tid.drt
group by ta.db_name,ta.str_norm_number 
order by ta.str_norm_number desc





CREATE TABLE IF NOT EXISTS `test` (

`id` int(10) unsigned NOT NULL auto_increment,
`install` int(10) unsigned NOT NULL,
`day` int(10) unsigned NOT NULL,
`aid` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=12 ;


INSERT INTO `test` (`id`, `install`, `day`, `aid`) VALUES
(1, 1232, 20080808, 1),
(2, 2321, 20080809, 2),
(3, 1236, 20080810, 3),
(5, 4212, 20080809, 1),
(6, 2312, 20080810, 1),
(7, 1432, 20080811, 1),
(8, 2421, 20080808, 2),
(9, 4245, 20080811, 2),
(10, 5654, 20080810, 2),
(11, 412, 20080808, 3);




SELECT A.* FROM test A,
(SELECT aid, MAX(day) max_day FROM test GROUP BY aid) B
WHERE A.aid = B.aid AND A.day = B.max_day

ORDER BY a.install DESC



网络上搜的到的:http://blog.csdn.net/acmain_chm/article/details/4126306

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值