先来讲一下需求:本文要查的是最近有数据的10天的记录,假如数据库中的字段create_time= 2016/9/26有三条数据,create_time= 2016/9/24有两条数据,那么26号,24号就是前两天有数据的记录,以此类别,查询最近有数据的10天的记录。
先根据日期分组
SELECT created_at FROM tableName group by DATE_FORMAT(created_at,'%m-%d-%Y')
根据日期从大到小排序,找到最小的日期
(SELECT created_at FROM `business` group by DATE_FORMAT(created_at,'%m-%d-%Y') order by created_at desc limit 9,1)
You can use LIMIT 2,1 instead of WHERE row_number() = 3.
As the documentation explains, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return.
Keep in mind that it’s an 0-based index. So, if you want the line number n, the first argument should be n-1. The second argument will always be 1, because you just want one row. For example, if you want the line number 56 of a table customer:
SELECT * FROM customer LIMIT 55,1
将刚刚查询到的日期作为下次查询的条件
Select * FROM `business` where created_at >= (SELECT created_at FROM `business` group by DATE_FORMAT(created_at,'%m-%d-%Y') order by created_at desc limit 9,1) order by created_at desc