踩坑经历(九)一条双层循环的SQL实现业务需求

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lwl2014100338/article/details/84141479
业务场景
类目 背景
数据特点 表没有唯一主键,相同id可能有很多条
需求 取每条数据记录的最新记录
SQL实现

(1)利用双层循环嵌套实现


	SELECT * from bond_abs_two_update a where a.systime=
	(
		SELECT systime from bond_abs_two_update b where a.id=b.id group by systime DESC LIMIT 1
	) ORDER by id

(2)利用双层循环和count()函数实现


	SELECT * from bond_abs_two_update a where 
	(
		SELECT count(0) from bond_abs_two_update b where a.id=b.id and a.systime<b.systime
	)<1


猜你喜欢

转载自blog.csdn.net/lwl2014100338/article/details/84141479