***mysql中查询今天、昨天、上个月sql语句

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天Select * FROM 表名 Where TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名)
今天 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
#查询本季度数据
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 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 下月第一天
where   date(regdate)   =   curdate();
select   *   from   test   where   year(regdate)=year(now())   and   month(regdate)=month(now())   and   day(regdate)=day(now())
SELECT date( c_instime ) ,curdate( )
FROM `t_score`
WHERE 1
LIMIT 0 , 30

  


MSSQL获取昨天,本周,本月。。。

原文链接地址:http://blog.knowsky.com/186111.htm

特别说明下:以下统计本周数据时,星期天是作为下周的第一天,而不是本周最后一天,因此你把星期天作为本周最后一天时,你需要在getDate()的基础上减一天,如dateadd('day', -1, 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(qq,C_CALLTIME,getdate())=0

前半年1-6,后半年7-12:select * from table where datepart(mm,C_CALLTIME)/7 = datepart(mm,getdate())/7

昨天

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 时间字段 >= 时间字段

convert和dateadd函数结合使用就可以了。

用datediff(day,时间列,getdate())

上月

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

--------------------------------------------------------

1.现在我需要得到只是日期部分,时间部分不要,SQL怎么写?

select convert(varchar(10),getdate(),120)

2.求以下日期SQL:

昨天

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 时间字段 >= 时间字段

  

如何联系我:【万里虎】www.bravetiger.cn 【QQ】3396726884 (咨询问题100元起,帮助解决问题500元起) 【博客】http://www.cnblogs.com/kenshinobiy/
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
15天前
|
SQL
sql语句加正则 简化查询
sql语句加正则 简化查询
14 0
sql语句加正则 简化查询
|
20天前
|
关系型数据库 MySQL 数据库
轻松入门MySQL:精准查询,巧用WHERE与HAVING,数据库查询如虎添翼(7)
轻松入门MySQL:精准查询,巧用WHERE与HAVING,数据库查询如虎添翼(7)
|
20天前
|
缓存 关系型数据库 MySQL
MySQL查询优化:提速查询效率的13大秘籍(合理使用索引合并、优化配置参数、使用分区优化性能、避免不必要的排序和group by操作)(下)
MySQL查询优化:提速查询效率的13大秘籍(合理使用索引合并、优化配置参数、使用分区优化性能、避免不必要的排序和group by操作)(下)
|
20天前
|
缓存 关系型数据库 MySQL
MySQL 查询优化:提速查询效率的13大秘籍(索引设计、查询优化、缓存策略、子查询优化以及定期表分析和优化)(中)
MySQL 查询优化:提速查询效率的13大秘籍(索引设计、查询优化、缓存策略、子查询优化以及定期表分析和优化)(中)
|
5天前
|
SQL 关系型数据库 MySQL
mysql 数据库查询 查询字段用逗号隔开 关联另一个表并显示
mysql 数据库查询 查询字段用逗号隔开 关联另一个表并显示
17 2
|
7天前
|
关系型数据库 MySQL Shell
MySQL 查询
MySQL 查询
|
9天前
|
SQL 关系型数据库 MySQL
DQL语言之基础查询(mysql)
DQL语言之基础查询(mysql)
|
9天前
|
SQL 关系型数据库 MySQL
DQL语言之连接查询(mysql)
DQL语言之连接查询(mysql)
|
9天前
|
关系型数据库 MySQL
MySQL全局库表查询准确定位字段
information_schema.COLUMNS 详细信息查询
199 4
|
9天前
|
SQL 关系型数据库 数据库
【后端面经】【数据库与MySQL】SQL优化:如何发现SQL中的问题?
【4月更文挑战第12天】数据库优化涉及硬件升级、操作系统调整、服务器/引擎优化和SQL优化。SQL优化目标是减少磁盘IO和内存/CPU消耗。`EXPLAIN`命令用于检查SQL执行计划,关注`type`、`possible_keys`、`key`、`rows`和`filtered`字段。设计索引时考虑外键、频繁出现在`where`、`order by`和关联查询中的列,以及区分度高的列。大数据表改结构需谨慎,可能需要停机、低峰期变更或新建表。面试中应准备SQL优化案例,如覆盖索引、优化`order by`、`count`和索引提示。优化分页查询时避免大偏移量,可利用上一批的最大ID进行限制。
34 3