创建表:
create table test(uid int,
targer varchar(1),
status int,
date date)
insert into test values (1,'A',0,'2018-02-01')
insert into test values (1,'B',1,'2018-01-01')
insert into test values (2,'A',0,'2018-02-01')
insert into test values (2,'B',0,'2018-01-01')
执行:
select t1.uid,max(case when t1.status=1 then t1.target else t2.target end) targer from test t1,(select a.* from test a,
(select uid,max(date) date from test group by uid) b
where a.uid=b.uid and a.date=b.date) t2
where t1.uid=t2.uid
group by t1.uid
结果:
如果混杂其他数据的话,另说。
SELECT DATE_FORMAT(create_time,'%Y-%m-%d') days,COUNT(id) COUNT FROM role GROUP BY days先转换xxxx-xx-xx时间格式,然后按时间格式进行分组。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)