MYSQL查询一周内的数据(最近7天的)的方法

这个方法是在网上找到的,因为在lofter山寨计划中写到一个功能块的时候需要用到这种查询方法,所以skidu将它记录下来

查询一天:

select * from table where to_days(column_time) = to_days(now());
select * from table where date(column_time) = curdate();


查询一周:

select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY)
 <= date(column_time);

查询一个月:

select * from table where
 DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH)
 <= date(column_time);

占用宝地,再记录两条MySQL获取随机内容的语句

SELECT * FROM `table`
WHERE id >=
(SELECT floor( RAND() * ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`)) +
(SELECT MIN(id) FROM `table`)))
ORDER BY id LIMIT 1;
SELECT *
FROM `table` AS t1 JOIN
(SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `table`)-(SELECT MIN(id) FROM `table`))+
(SELECT MIN(id) FROM `table`))
AS id) AS t2
WHERE t1.id >= t2.id
ORDER BY t1.id LIMIT 1;

标签: mysql, 最近一周, 最近七天

已有 2 条评论

  1. 掸落往日忙碌的尘埃,尽享亲人团聚的温馨,抚平工作中心灵的创伤,树立再次扬帆的信心。除夕又至,让好运为生活伴奏,愿你来年快乐久久

  2. 新年快乐!
    辛卯年(兔)腊月廿十 2012-1-13

评论已关闭