select * from wap_content where week(created_at) = week(now)
如果要严格要求是某一年的,那可以这样
查询一天:
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)
查询一年:
select * from table where DATE_SUB(CURDATE(), INTERVAL 1 YEAR) <= date(column_time)
扩展资料
mysql查询最近7天的数据:
1,(以当天为起点)
SELECT * FROM tb_equity e where DATE_SUB(CURDATE(), INTERVAL 6 DAY) <=
date(createdate)
2,(以数据库最新的时间最为最近的一天)
SELECT * FROM tb_equity e where createdate >DATE_ADD((select createdate from tb_equity
ORDER BY createdate DESC limit 1) ,INTERVAL -7 day)
and (select createdate from tb_equity ORDER BY createdate DESC limit 1) >= createdate
3,sql查询表中的重复数据
select * from 表名 where 字段名 in (select 字段名 from 表名 group by 字段名 HAVING COUNT(*)
>1) order by 表名
参考资料来源:百度百科 - 结构化查询语言
参考资料来源:百度百科 - mySQL (关系型数据库管理系统)
参考资料来源:百度百科 - select (Linux 网络编程)
-- by sleest 2018/01/19 以周一作为一周开始-- 假设条件日期为2018-01-19, 以下列举取上周始末和周几
select date_add(20180119, interval -7 - weekday(20180119) day) as last_week_start,
date_add(20180119, interval -1 - weekday(20180119) day) as last_week_end,
date_add(20180119, interval -8 - weekday(20180119) + 1 day) as last_week_monday,
date_add(20180119, interval -8 - weekday(20180119) + 2 day) as last_week_tuesday,
-- ...
date_add(20180119, interval -8 - weekday(20180119) + 7 day) as last_week_sunday
-- 取上周所有, 如果creationDate时间部分为00:00:00.0 可以直接小于等于上述last_week_end
select 1
from student
where creationDate >= date_add(20180119, interval -7 - weekday(20180119) day)
and creationDate < date_add(20180119, interval - weekday(20180119) day)
-- 取上周二, 如果creationDate时间部分为00:00:00.0 可以直接用一个等于条件
select 1
from student
where creationDate >= date_add(20180119, interval -8 - weekday(20180119) + 2 day)
and creationDate < date_add(20180119, interval -8 - weekday(20180119) + 3 day)
假设members 表里有个字段是日期(CDATA),和一个ID字段,要统计每天的条数,可以用下面的语句:SELECT i.CDATA,count(*) from members i
where cdata>=date_sub(now(),interval dayofweek(now()) day) and cdata <= date_add(now(),interval 7-dayofweek(now()) day) group by i.CDATA
以上语句统计的是从这周的第一天(星期天)到这周的最后一天(星期六)每天的记录条数。
程序里取出来循环得到每天的条数(查询出来的结果已经按日期分好,只要读取就行)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)