select `name`,test,max(`time`) as lastTime
from tblname where `name`='test'
group by `name`,test
如果要取整条记录的会麻烦一些,必须有唯一标识列,如主键、自增id等才可以实现,亲参考下列写法:
select * from tblname where id in
(select max(a.id) from tblname a,
(select `name`,test,max(`time`) as lastTime
from tblname where `name`='test'
group by `name`,test) t
where a.`name`=t.`name and
a.test=t.test and a.`time`=t.maxTime
group by a.`name`,a.test)
SELECT * FROM `tbl` WHERE id IN (SELECT MAX(id) FROM `tbl`
WHERE createdat BETWEEN ADDDATE(DATE_FORMAT(NOW() ,"%Y-%m-%d"), INTERVAL -6 DAY) AND DATE_FORMAT(NOW(),'%Y-%m-%d')
GROUP BY DATE_FORMAT(createdat,'%Y-%m-%d')
)
前提你的表要有自增主键
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)