当前位置:数据库 > MySQL >>

mysql查询上月,本月,上周,本周记录的sql代码

 mysql获取上月,本月,上周,本周记录

select * from 表名 where to_days(时间字段名) = to_days(now());

昨天

SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) – TO_DAYS( 时间字段名) <= 1

7天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)

近30天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)

本月

SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, ‘%Y%m’ ) = DATE_FORMAT( CURDATE( ) , ‘%Y%m’ )

上一月

SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , ‘%Y%m’ ) , date_format( 时间字段名, ‘%Y%m’ ) ) =1

本季度的第一天 ,然后你可以把字符串再转换为日期。 方法很多,包括同一个函数的别名(同义词)也很多。比如CURDATE(),CURRENT_DATE(), CURRENT_DATE, NOW 等都可以返回当天

mysql教程> select CURDATE(),ELT(QUARTER(CURDATE()),
-> year(CURDATE())*1000+0101,
-> year(CURDATE())*1000+0401,
-> year(CURDATE())*1000+0701,
-> year(CURDATE())*1000+1001) as firstDayofQ;
+————+————-+
| CURDATE() | firstDayofQ |
+————+————-+
| 2009-05-19 | 2009401 |
+————+————-+
1 row in set (0.00 sec)

mysql>

用mysql语句获取本季度的第一天

本月的第一天,
date(
concat(year(curdate()),’-',month(curdate()),’-',’1′))
本周的第一天
curdate()-
WEEKDAY(curdate())

用mysql语句获取本季度的第一天

date(
concat(year(curdate()),’-',
if(quarter(curdate())=1,1,if(quarter(curdate())=2,4,if(quarter(curdate())=3,7,10))

用mysql语句获取本季度的第一天:
优化:
date(
concat(year(curdate()),’-',elt(quarter(curdate()),1,4,7,10),’-',1))

本周的第一天
date_add(date(’2009-5-29′),INTERVAL -weekday(’2009-5-29′) day)

mysql 获取当天发布文件内容sql语句

SELECT * FROM `p8_memberdata` where regdate BETWEEN (UNIX_TIMESTAMP(NOW())-86440) AND NOW()
SELECT * FROM `p8_memberdata` WHERE DATE_FORMAT(FROM_UNIXTIME(regdate),’%Y-%m-%d’)= DATE_FORMAT(NOW(),’%Y-%m-%d’) order by uid desc

====================================================================

mysql 查询当天、本周、上周、本月、上个月份的数据 

2012-06-25 17:52:45|  分类: 默认分类 |举报|字号 订阅
查询当天的记录
select * from hb_article_view where TO_DAYS(hb_AddTime) = TO_DAYS(NOW())
查询当前这周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,’%Y-%m-%d’)) = YEARWEEK(now());
查询上周的数据
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,’%Y-%m-%d’)) = YEARWEEK(now())-1;
查询当前月份的数据
select name,submittime from enterprise where date_format(submittime,’%Y-%m’)=date_format(now(),’%Y-%m’)
查询距离当前现在6个月的数据
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查询上个月的数据
select name,submittime from enterprise where date_format(submittime,’%Y-%m’)=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),’%Y-%m’)
select * from `user` where DATE_FORMAT(pudate,’%Y%m’) = DATE_FORMAT(CURDATE(),’%Y%m’) ;
select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,’%y-%m-%d’)) = WEEKOFYEAR(now())
select *
from user
where MONTH(FROM_UNIXTIME(pudate,’%y-%m-%d’)) = MONTH(now())
select *
from [user]
where YEAR(FROM_UNIXTIME(pudate,’%y-%m-%d’)) = YEAR(now())
and MONTH(FROM_UNIXTIME(pudate,’%y-%m-%d’)) = MONTH(now())
select *
from [user]
where pudate between 上月最后一天
and 下月第一天

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,