大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
查询本周记录
成都创新互联公司是一家专业提供广陵企业网站建设,专注与成都网站设计、网站制作、外贸营销网站建设、H5页面制作、小程序制作等业务。10年已为广陵众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。
select * from ht_invoice_information where WEEKOFYEAR(create_date)=WEEKOFYEAR(NOW());
#查询上周记录
select * from ht_invoice_information where create_date=date_add(now(),interval -(8 + weekday(now())) day)
and create_date=date_add(now(),interval -(1 + weekday(now())) day);
#或者
select * from `ht_invoice_information` where WEEKOFYEAR(create_date)=WEEKOFYEAR(DATE_SUB(now(),INTERVAL 1 week));
#查询本月数据
select * from ht_invoice_information where MONTH(create_date)=MONTH(NOW()) and year(create_date)=year(now());
#查询上月数据
select * from ht_invoice_information where create_date=last_day(date_add(now(),interval -1 MONTH))
and create_date=DATE_FORMAT(concat(extract(year_month from date_add(now(),interval -1 MONTH)),'01'),'%Y-%m-%d');
#或者
select * from `ht_invoice_information` where MONTH(create_date)=MONTH(DATE_SUB(NOW(),interval 1 month))
and year(create_date)=year(now());
#查询本季度数据
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
#查询上季度数据
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
#查询本年数据
select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
#查询上年数据
select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));
参考这个吧,很全了。
select curDate(); #获取当前日期select curTime(); #获取当前时间select now(); #获取当前日期+时间
列举1个天数加减的例子,其他的看英文意思就可以理解了
select date_add(now(), interval 1 day); #当前日期天数+1
select date_add(now(), interval -1 day); #当前日期天数-1
select date_add(now(), interval 1 hour);
select date_add(now(), interval 1 minute);
select date_add(now(), interval 1 second);
select date_add(now(), interval 1 microsecond);
select date_add(now(), interval 1 week);
select date_add(now(), interval 1 month);
select date_add(now(), interval 1 quarter);
select date_add(now(), interval 1 year);
本季度的第一天 ,然后你可以把字符串再转换为日期。 方法很多,包括同一个函数的别名(同义词)也很多。比如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语句获取本季度的第一天
您好,一、年度查询
查询 本年度的数据
SELECT *
FROM blog_article
WHERE year( FROM_UNIXTIME( BlogCreateTime ) ) = year( curdate( ))
二、查询季度数据
查询数据附带季度数
SELECT ArticleId, quarter( FROM_UNIXTIME( `BlogCreateTime` ) )
FROM `blog_article`
其他的同前面部分:查询 本季度的数据
SELECT *
FROM blog_article
WHERE quarter( FROM_UNIXTIME( BlogCreateTime ) ) = quarter( curdate( ))
三、查询月度数据
本月统计(MySQL)
select * from booking where month(booking_time) =
month(curdate()) and year(booking_time) = year(curdate())
本周统计(MySQL)
select * from spf_booking where month(booking_time) =
month(curdate()) and week(booking_time) = week(curdate())
四、时间段
N天内记录
WHERE TO_DAYS(NOW()) - TO_DAYS(时间字段) = N
当天的记录
where date(时间字段)=date(now())
或
where to_days(时间字段) = to_days(now());
查询一周:
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);
查询'06-03'到'07-08'这个时间段内所有过生日的会员:
Select * From user Where
DATE_FORMAT(birthday,'%m-%d') = '06-03' and DATE_FORMAT(birthday,'%m-%d')
= '07-08';
统计一季度数据,表时间字段为:savetime
group by concat(date_format(savetime, '%Y '),FLOOR((date_format(savetime, '%m ')+2)/3))
或
select YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1,count(*)
from yourTable
group by YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1;
五、分组查询
1、年度分组
2、月度分组
3、先按年度分组,再按月度分组
4、按年月分组
SELECT count(ArticleId), date_format(FROM_UNIXTIME( `BlogCreateTime`),'%y%m') sdate FROM `blog_article` group by sdate
结果:
count( ArticleId ) sdate
17 0901
11 0902
5 0903
6 0904
2 0905
1 0907
12 0908
6 0909
11 0910
3 0911
俩方法
select year(订单.订购日期)年份,
sum(case when month(订单.订购日期) between 1 and 3 then 订单明细.单价*订单明细.数量 else 0 end) 一季度销售金额,
sum(case when month(订单.订购日期) between 4 and 6 then 订单明细.单价*订单明细.数量 else 0 end) 二季度销售金额,
sum(case when month(订单.订购日期) between 7 and 9 then 订单明细.单价*订单明细.数量 else 0 end) 三季度销售金额,
sum(case when month(订单.订购日期) between 10 and 12 then 订单明细.单价*订单明细.数量 else 0 end) 四季度销售金额
from 订单,订单明细
where 订单.订单ID=订单明细.订单ID and year(订单.订购日期) between 1996 and 1998
group by year(订单.订购日期)
select year(订单.订购日期) 年份,
case when month(订单.订购日期) between 1 and 3 then '一季度'
when month(订单.订购日期) between 4 and 6 then '二季度'
when month(订单.订购日期) between 7 and 9 then '三季度'
when month(订单.订购日期) between 10 and 12 then '四季度' end 季度,
sum(订单明细.单价*订单明细.数量) 金额
from 订单,订单明细
where 订单.订单ID=订单明细.订单ID and year(订单.订购日期) between 1996 and 1998
group by year(订单.订购日期),
case when month(订单.订购日期) between 1 and 3 then '一季度'
when month(订单.订购日期) between 4 and 6 then '二季度'
when month(订单.订购日期) between 7 and 9 then '三季度'
when month(订单.订购日期) between 10 and 12 then '四季度' end
你看你要用哪个