Mysql 求时间 between 昨天 and 上个月的今天 等时间函数
问题:
Mysql表中一列create_time,类型datetime(YYYY-MM-DD HH:MM:SS),想获取上个月今天到昨天的数据。
select * from 表名 where date_format(create_time,'%Y-%m-%d') between date_sub(curdate(),interval 1 MONTH) and date_sub(curdate(),interval 1 day)
仔细研究了下,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(时间字段);
/*查询距离当前现在6个月的数据*/
select * from 表名 where 时间字段 between date_sub(now(),interval 6 month) and now();
/*查询当前这周的数据*/
select * from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now());
/*查询上周的数据*/
select * from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now())-1;
/*查询当前月份的数据*/
select * from 表名 where date_format(时间字段,'%Y-%m')=date_format(now(),'%Y-%m');
/*查询上个月的数据*/
select * from 表名 where date_format(时间字段,'%Y-%m')=date_format(date_sub(curdate(), interval 1 month),'%Y-%m');
Mysql 求时间 between 昨天 and 上个月的今天 等时间函数的更多相关文章
- js对时间戳的处理 获取时间,昨天,今天,明天,时间不同格式
1.获取昨天,今天,明天的时间 //昨天的时间 var day1 = new Date(); day1.setTime(day1.getTime()-24*60*60*1000); var s1 = ...
- mysql 查询今天,昨天,上个月sql语句 注解
今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天Select * FROM 表名 Where TO_DAYS( NOW( ) ...
- mysql 查询今天,昨天,上个月sql语句
今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天Select * FROM 表名 Where TO_DAYS( NOW( ) ...
- mysql中获取一天、一周、一月时间数据的各种sql语句写法
今天抽时间整理了一篇mysql中与天.周.月有关的时间数据的sql语句的各种写法,部分是收集资料,全部手工整理,自己学习的同时,分享给大家,并首先默认创建一个表.插入2条数据,便于部分数据的测试,其中 ...
- mysql查询今天、昨天、上周
mysql查询今天.昨天.上周 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE ...
- mysql查询今天、昨天、近7天、近30天、本月、上一月的SQL语句
mysql查询今天.昨天.近7天.近30天.本月.上一月的SQL语句 这篇文章主要介绍了mysql查询今天.昨天.近7天.近30天.本月.上一月的SQL语句,一般在一些统计报表中比较常用这个时间段,需 ...
- mysql 查询当天、昨天、本周、上周、本月、上月、今年、去年数据
mysql查询今天.昨天.7天.近30天.本月.上一月 数据 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT ...
- [moka同学笔记]php 获取时间(今天,昨天,三天内,本周,上周,本月,三年内,半年内,一年内,三年内)
<?php /** * php 获取时间(今天,昨天,三天内,本周,上周,本月,三年内,半年内,一年内,三年内) * * author:ihelloworld2010@gmail.com * d ...
- mysql取出现在的时间戳和时间时间戳转成人类看得懂的时间
mysql取出现在的时间戳和时间时间戳转成人类看得懂的时间,我们在mysql里面他封装了一个内置的时间戳转化的函数,比如我们现在的时间戳是:1458536709 ,"%Y-%m-%d&quo ...
随机推荐
- day03-Linux操作系统目录结构
一. Linux系统目录表示 ‘/’表示根目录: ‘.’表示当前目录 ...
- vue原理实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- iOS10 新特性-新功能,以及ReplayKit库
iOS的10.0 本文总结了iOS的10,运行于目前推出iOS设备推出的主要开发者相关的功能. iOS版10引入了新的方法来增加您的应用程序通过帮助系统参与在适当的时候建议你的应用程序给用户.如果你在 ...
- 20.Add Two Numbers(两个链表的和)
Level: Medium 题目描述: You are given two non-empty linked lists representing two non-negative integer ...
- zabbix+telegram的API接口(告警)
首先在telegram里创建一个有API接口的用户,创建是在 @BotFather 选择/start——————/newbot 输入机器人的用户名,根据提示操作.获得bot的API接口和群ID 通 ...
- mongoDB3.4安装
添加官方yum源——mongodb3.4vim /etc/yum.repos.d/mongodb-org-3.4.repo [mongodb-org-3.4] name=MongoDB Reposit ...
- zookeeper客户端使用第三方(zkclient)封装的Api操作节点
1.引入依赖 <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</a ...
- 8. sql 片段
sql 片段: <sql id="columnBase"> `id`, `title`, `author_id` as authorId, `state`, `feat ...
- ubuntu下vnc部署安装
ubuntu下vnc部署安装,参考如下博客:https://www.cnblogs.com/xuliangxing/p/7642650.html https://jingyan.baidu.com/a ...
- Tcpdump 的详细用法
1.抓取回环网口的包:tcpdump -i lo 2.防止包截断:tcpdump -s0 3.以数字显示主机及端口:tcpdump -n 第一种是关于类型的关键字,主要包括host,net,port, ...