cookie和session区别_SQL数据库下载

(25) 2024-10-04 14:01:01

sql 语句查询今天、昨天、近7天、近30天、一个月内、上一月 数据


MySQL

查询当天的所有数据

 SELECT * FROM 表名 WHERE DATEDIFF(字段,NOW()) = 0 SELECT * FROM 表名 WHERE to_days(时间字段名) = to_days(now()); 

查询昨天的所有数据

SELECT * FROM 表名 WHERE DATEDIFF(字段,NOW())=-1 SELECT * FROM 表名 WHERE TO_DAYS(NOW()) - TO_DAYS(时间字段名) = 1

查询未来第n天的所有数据

//当n为负数时,表示过去第n天的数据 SELECT * FROM 表名WHERE DATEDIFF(字段,NOW())=n

查询未来n天内所有数据

 //n天内 SELECT * FROM 表名 WHERE DATEDIFF(字段,NOW())<n AND DATEDIFF(字段,NOW())>=

查询出今天,年月日

SELECT DATE_SUB(CURDATE(), INTERVAL 0 DAY)

几个小时内的数据

DATE_SUB(NOW(), INTERVAL 5 HOUR)

7天

SELECT * FROM 表名 wher 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

查询过去n天内所有数据

//包含当天 SELECT * FROM 表名 WHERE DATEDIFF(字段,NOW())<=0 AND DATEDIFF(字段,NOW())>-n //不包含当天 SELECT * FROM 表名 WHERE DATEDIFF(字段,NOW())<0 AND DATEDIFF(字段,NOW())>-n

常用统计

/** 当天 */ SELECT COUNT(*),'当天' as title FROM tb_device_status WHERE DATEDIFF(rawIpUpDatetime,NOW()) = 0 UNION ALL /** 本周 */ SELECT COUNT(*),'本周' as title FROM tb_device_status WHERE YEARWEEK(date_format(rawIpUpDatetime,'%Y-%m-%d'),1) = YEARWEEK(now(),7) UNION ALL /** 本月 */ SELECT COUNT(*),'本月' as title FROM tb_device_status WHERE DATE_FORMAT(rawIpUpDatetime, '%Y-%m') = DATE_FORMAT(now(),'%Y-%m') UNION ALL /** 本季度 */ select COUNT(*),'本季度' as title from tb_device_status where QUARTER(rawIpUpDatetime)=QUARTER(now()) and year(rawIpUpDatetime)=year(now()) UNION ALL /** 本年 */ SELECT COUNT(*),'本年' as title FROM tb_device_status where DATE_FORMAT(rawIpUpDatetime,'%Y') = DATE_FORMAT(SYSDATE(),'%Y')

cookie和session区别_SQL数据库下载 (https://mushiming.com/)  第1张


SQL SERVER

当天:

select * from T_news where datediff(day,addtime,getdate())=0

昨天

select * from T_news where datediff(day, 时间字段 ,getdate()) = 1

三天

select * from T_news where datediff(day,addtime,getdate())<= 2 and datediff(day,addtime,getdate())>= 0

一周 (注意:此时不能用 datediff 差值为7,因为,datediff只表示间隔数)

select * from T_news WHERE (DATEPART(wk, addtime) = DATEPART(wk, GETDATE())) AND (DATEPART(yy, addtime) = DATEPART(yy, GETDATE()))

一月

select * from T_news WHERE (DATEPART(yy, addtime) = DATEPART(yy, GETDATE())) AND (DATEPART(mm, addtime) = DATEPART(mm, GETDATE()))

一季度 

 select * from T_news where DATEPART(, addtime) = DATEPART(, GETDATE()) and DATEPART(yy, addtime) = DATEPART(yy, GETDATE())

本周

select * from table where datediff(week,C_CALLTIME,getdate())=0 --C_CALLTIME 为日期字段

本月

select * from table where datediff(Month,C_CALLTIME,getdate())=0 --C_CALLTIME 为日期字段

本季

select * from table where datediff(,C_CALLTIME,getdate())=0

前半年1-6,后半年7-12

select * from table where datepart(mm,C_CALLTIME)/7 = datepart(mm,getdate())/7

 

sql得到当前系统时间得 日期部分

CONVERT(varchar(10),getDate(),120)
昨天 select convert(varchar(10),getdate() - 1,120) 明天 select convert(varchar(10),getdate() + 1,120) 最近七天 select * from tb where 时间字段 >= convert(varchar(10),getdate() - 7,120) 随后七天 select * from tb where 时间字段 <= convert(varchar(10),getdate() + 7,120) and 时间字段 >= 时间字段 上月 select * from tb where month(时间字段) = month(getdate()) - 1 本月 select * from tb where month(时间字段) = month(getdate()) 下月 select * from tb where month(时间字段) = month(getdate()) + 1 --昨天 Select * From TableName Where DateDiff(dd, DateTimCol, GetDate()) = 1 --明天 Select * From TableName Where DateDiff(dd, GetDate(), DateTimCol) = 1 --最近七天 Select * From TableName Where DateDiff(dd, DateTimCol, GetDate()) <= 7 --随后七天 Select * From TableName Where DateDiff(dd, GetDate(), DateTimCol) <= 7 --上周 Select * From TableName Where DateDiff(wk, DateTimCol, GetDate()) = 1 --本周 Select * From TableName Where DateDiff(wk, DateTimCol, GetDate()) = 0 --下周 Select * From TableName Where DateDiff(wk, GetDate(), DateTimCol ) = 1 --上月 Select * From TableName Where DateDiff(mm, DateTimCol, GetDate()) = 1 --本月 Select * From TableName Where DateDiff(mm, DateTimCol, GetDate()) = 0 --下月 Select * From TableName Where DateDiff(mm, GetDate(), DateTimCol ) = 1 -------------------------------------------------------- 本周 select * from tb where datediff(week , 时间字段 ,getdate()) = 0 上周 select * from tb where datediff(week , 时间字段 ,getdate()) = 1 下周 select * from tb where datediff(week , 时间字段 ,getdate()) = -1 

2021-12-12 补充

SQLserver获取本周、本月、本季、本年、第一天、最后一天

注:本周第一天为周一、最后一天为周日(七)

--本周第一天 select dateadd(WEEK, datediff(WEEK, 0, getdate()), 0); --本周最后一天 select dateadd(WEEK, datediff(WEEK, 0, getdate()), 6); --上周第一天 select dateadd(WEEK, datediff(WEEK, 0, getdate())-1, 0); --上周最后一天 select dateadd(WEEK, datediff(WEEK, 0, getdate())-1, 6); --本月第一天 select dateadd(month, datediff(month, 0, getdate()), 0); --本月最后一天 select dateadd(month, datediff(month, -1, getdate()), -1); --上月第一天 select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) --上月最后一天 select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1) --本季第一天 select dateadd(QUARTER , datediff(QUARTER ,0, getdate()), 0); --本季最后一天 select dateadd(QUARTER , datediff(QUARTER , -1, getdate()), -1); --上季第一天 select dateadd(QUARTER , datediff(QUARTER ,0, getdate())-1, 0); --上季最后一天 select dateadd(QUARTER , datediff(QUARTER , -1, getdate())-1, -1); --本年第一天 select dateadd(year, datediff(year, 0, getdate()), 0); --本年最后一天 select dateadd(year, datediff(year, -1, getdate()), -1); --上年第一天 select dateadd(year, datediff(year, 0, getdate()), 0); --上年最后一天 select dateadd(year, datediff(year, 0, getdate()), -1); 


原文链接

sql 语句查询今天、昨天、近7天、近30天、一个月内、上一月 数据_allWords的博客-CSDN博客_sql查询最近一个月的数据

SQLServer 查询最近一天,三天,一周,一月,一季度数据的方法_Sun_Goddess的博客-CSDN博客

sql语句根据日期查询,本周,本月,本年,今日相关统计 - xszjk - 博客园

SQLserver获取本周、本月、本季、本年、第一天、最后一天

THE END

发表回复