我的需求是在MySQL中使用LIMIT获取最后5条数据,也就是按照时间正序的最新日期的5条数据
受后端拼接字符串查询条件限制,只能修改SQL语句的参数,不能增加任何的条件语句,现在的语句是
SELECT * FROM ( select ${calculation} ${date} from ${main} ${car} ${chackpoint} ${dep} ${cycle})V ORDER BY V.date
//代入拼接的内容是
SELECT * FROM (SELECT 计算条件及相关需求值`,WEEK AS date ,riqi FROM 表名 where 条件 AND DATE_SUB(CURDATE(), INTERVAL 7 MONTH) <= date(riqi) GROUP BY `WEEK` ORDER BY riqi desc LIMIT 0,5 ) V ORDER BY V.date
//可修改部分为
AND DATE_SUB(CURDATE(), INTERVAL 7 MONTH) <= date(riqi) group by WEEK ORDER BY riqi DESC LIMIT 0,5
现在获取的5条数据,但是时间顺序不对
需要的顺序是
*重点:只能修改下面这一句的内容
AND DATE_SUB(CURDATE(), INTERVAL 7 MONTH) <= date(riqi) group by WEEK ORDER BY riqi DESC LIMIT 0,5
PS:
1.原表中数据是按照时间倒序排列,但是经过group by WEEK后riqi列顺序就变了
2.表中的WEEK列不是提取的系统时间,通过时间类的函数无法对WEEK列进行操作