#今天
select *
from or_order_task where to_days(created_date)=to_days(now());
#近七天
select *
from or_order_task where date_sub(curdate(),interval 7 day )<=date(created_date);
#近七天
select *
from or_order_task where date_sub(curdate(),interval 30 day )<=date(created_date);
#本月
select *
from or_order_task
where date_format(created_date, '%Y%m') = date_format(curdate(), '%Y%m');
#以月为纬度,取得上个月的数据 如当前时间为2018-07-02 则取数据范围为所有数据中满足字段名中月份为08-01至08-31的数据(可用于筛选下个月生日人员) 
#数据搜索范围可通过最后判断的值进行调整 0为当月 1是上个月 -1为下个月 以此类推
SELECT created_date FROM or_order_task WHERE PERIOD_DIFF(DATE_FORMAT(CURDATE(),'%m'),DATE_FORMAT(created_date,'%m'))= 1;
# 以年月为维度 取得下个月数据
SELECT *FROM or_order_task WHERE PERIOD_DIFF(DATE_FORMAT(CURDATE(),'%Y%m'),DATE_FORMAT(now(),'%Y%m'))=-1
#查询本季度数据
select * from `ticket_order_detail` where QUARTER(use_time)=QUARTER(now());
#查询上季度数据
select * from `ticket_order_detail` where QUARTER(use_time)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
#查询本年数据
select * from `ticket_order_detail` where YEAR(use_time)=YEAR(NOW());
#查询上年数据
select * from `ticket_order_detail` where year(use_time)=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’)
-- 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

mysql 时间相关sql , 按天、月、季度、年等条件进行查询的更多相关文章

  1. sql having 函数 按匿名字段作为条件进行查询

    今天写sql 遇到一个问题 SELECT a.*, count(b.id) AS nums FROM a LEFT JOIN b ON a.id=b.a_id WHERE nums>1 这时候会 ...

  2. SQL中以count及sum为条件的查询

    在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7   例1:查询出现 ...

  3. SQL中以count或sum为条件的查询方式

    在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7   例1:查询出现 ...

  4. mysql中SQL执行过程详解与用于预处理语句的SQL语法

    mysql中SQL执行过程详解 客户端发送一条查询给服务器: 服务器先检查查询缓存,如果命中了缓存,则立刻返回存储在缓存中的结果.否则进入下一阶段. 服务器段进行SQL解析.预处理,在优化器生成对应的 ...

  5. oracle 、mysql、 sql server使用记录

    oracle .mysql. sql server使用记录 mysql常用命令: mysqld --启动mysql数据库 show databases; -- 查看数据库 use database; ...

  6. Mysql中用SQL增加、删除、修改(包括字段长度/注释/字段名)总结

    转: Mysql中用SQL增加.删除.修改(包括字段长度/注释/字段名)总结 2018年09月05日 10:14:37 桥Dopey 阅读数:1830   版权声明:本文为博主原创文章,未经博主允许不 ...

  7. Oracle和MySql之间SQL区别(等效转换以及需要注意的问题)

    本篇博文是Oracle和MySQL之间的等效SQL转换和不同,目前市面上没有转换两种SQL的工具,小编觉得以后也不一定会有,于是在业余时间整理了一下,如果有什么错误之处请留言告知,小编也是刚入门的小白 ...

  8. 小麦苗数据库巡检脚本,支持Oracle、MySQL、SQL Server和PG等数据库

    目录 一.巡检脚本简介 二.巡检脚本特点 三.巡检结果展示 1.Oracle数据库 2.MySQL数据库 3.SQL Server数据库 4.PG数据库 5.OS信息 四.脚本运行方式 1.Oracl ...

  9. Mysql 常用 SQL 语句集锦

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

随机推荐

  1. Golang Gin 项目使用 Swagger

    Golang Gin 项目使用 Swagger 标签(空格分隔): Go 首先需要github.com/swaggo/gin-swagger和github.com/swaggo/gin-swagger ...

  2. php导出cvs xls xlsx

    有两种方法,一种是更改输出头部,一种是使用phpexcel类,很显然前者更方便,下面给出一个demo方法导出cvs/** * 导出日志 */public function excel() { setl ...

  3. March 27 2017 Week 13 Monday

    A book that remains shut is but a block. 有书闭卷不阅读,无异于一块木头. I had planned to buy a book and read it ev ...

  4. 设计模式——抽象工厂模式(AbstractFactoryPattern)

    抽象工厂模式(AbstractFactory):提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类. UML图: IFactory: package com.cnblog.clarck ...

  5. SQL SERVER启动时间

    select sqlserver_start_time,* from sys.dm_os_sys_info

  6. python:序列与模块

    一,序列化模块 什么叫序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 比如,我们在python代码中计算的一个数据需要给另外一段程序使用,那我们怎么给? 现在我们能想到的方法就 ...

  7. [18/11/26] this关键字、static关键字和静态块(及语句块)

    1.this关键字 this的本质就是“创建好的对象的地址”!  由于在构造方法调用前,对象已经创建.因此,在构造方法中也可以使用this代表“当前对象” [用法]   1.  在程序中产生二义性之处 ...

  8. java json和对象互转

    开发过程中遇到一些对象转string和string转对象的问题,浪费了很久,现在用的熟练些了,总结如下: 1.字符串尽量定义成json可解析的,如{"name":"a&q ...

  9. 188. Best Time to Buy and Sell Stock IV——LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  10. <body> 中的 JavaScript

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...