mysql 求最小时间记录,mysql-获取每个人每天的最小日期时间的记录

CREATE TABLE IF NOT EXISTS `accesscards` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`department` varchar(255) NOT NULL,

`name` varchar(255) NOT NULL,

`entrydates` datetime NOT NULL, PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

INSERT INTO `accesscards` (`id`, `department`, `name`, `entrydates`) VALUES

(1, 'test', 't1', '2013-12-06 16:10:00'),

(2, 'test', 't1', '2013-12-06 15:10:00'),

(3, 'test', 't1', '2013-12-07 15:11:00'),

(4, 'test', 't1', '2013-12-07 15:24:00'),

(5, 'test', 't2', '2013-12-06 16:10:00'),

(6, 'test', 't2', '2013-12-06 16:25:00'),

(7, 'test', 't2', '2013-12-07 15:59:00'),

(8, 'test', 't2', '2013-12-07 16:59:00');

上面是我的查询,我想获取一个人每天的记录.该记录应具有当天的最小日期时间.我需要该日期时间的完整记录

我的预期输出here

我尝试使用

SELECT id, MIN(entrydates) FROM accesscards WHERE 1=1 AND name!='' GROUP BY DATE(entrydates) ORDER BY id

但是对于“ t1”,我得到id = 1和第一行的输入日期.

请帮帮我.如果重复,则提供链接.

解决方法:

SELECT a1.*

FROM accesscards a1

JOIN (SELECT name, MIN(entrydates) mindate

FROM accesscards

WHERE name != ''

GROUP BY name, date(entrydates)) a2

ON a1.name = a2.name AND a1.entrydates = a2.mindate

标签:greatest-n-per-group,sql,mysql

来源: https://codeday.me/bug/20191122/2058087.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值