Chinaunix首页 | 论坛 | 博客
  • 博客访问: 763602
  • 博文数量: 116
  • 博客积分: 923
  • 博客等级: 准尉
  • 技术积分: 1635
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-06 21:43
个人简介

一直帮老板搬运代码!!!

文章分类
文章存档

2013年(47)

2012年(69)

分类: LINUX

2012-07-20 15:19:17

查询本周记录
select * from `tableName` where WEEKOFYEAR(date_field)=WEEKOFYEAR(NOW());
#查询上周记录
select * from `tableName` where date_field>=date_add(now(),interval -(8 + weekday(now())) day)
and date_field<=date_add(now(),interval -(1 + weekday(now())) day);
#或者
select * from `tableName` where WEEKOFYEAR(date_field)=WEEKOFYEAR(DATE_SUB(now(),INTERVAL 1 week));
//前一天
select date_sub(now(),interval 1 day);
Select * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)
近30天前
//时间转成年月日
select date_format(now(),'%Y%m%d')
本月
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
查询当前月份的数据
select name,submittime from enterprise   where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')
查询当前这周的数据
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;
近30天前
Select * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)
#查询上月数据
select * from `tableName` where date_field<=last_day(date_add(now(),interval -1 MONTH))
and date_field>=DATE_FORMAT(concat(extract(year_month from date_add(now(),interval -1 MONTH)),'01'),'%Y-%m-%d');
#或者
select * from `tableName` where MONTH(date_field)=MONTH(DATE_SUB(NOW(),interval 1 month))
and year(date_field)=year(now());

 
阅读(833) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~