博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql 查询当天、本周,本月,上一个月的数据
阅读量:5740 次
发布时间:2019-06-18

本文共 5103 字,大约阅读时间需要 17 分钟。

hot3.png

今天

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,时间字段名FROM enterprise WHERE YEARWEEK(date_format(时间字段名,'%Y-%m-%d')) = YEARWEEK(now());

查询上周的数据

SELECT name,时间字段名FROM enterprise WHERE YEARWEEK(date_format(时间字段名,'%Y-%m-%d')) = YEARWEEK(now())-1;

查询上个月的数据

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 下月第一天

查询当前月份的数据

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    COUNT(1) AS countNumber,    DATE_FORMAT(createTime,'%Y-%m-%d') AS dateTimeFROM    testTableGROUP BY DATE_FORMAT(createTime,'%Y-%m-%d')

查询每周的数据:

SELECT    COUNT(1) AS countNumber,    WEEK(createTime) as dateTimeFROM    testTableGROUP BY WEEK(createTime)

查询每月的数据:

SELECT    COUNT(1) AS countNumber,    MONTH(createTime) as dateTimeFROM    testTableGROUP BY MONTH(createTime)

查询每年的数据:

SELECT    COUNT(1) AS countNumber,    YEAR(createTime) as dateTimeFROM    testTableGROUP BY YEAR(createTime)

其它:

DATE_ADD(createTime,INTERVAL 1 DAY) 得到指定日期后一天的日期/把1改为任意数字就可以得到后N天的日期

DATE_SUB(createTime,INTERVAL 1 DAY) 得到指定日期前一天的日期/把1改为任意数字就可以得到前N天的日期

声明变量查询:

SET @counter=0;

select :=@counter+1 as id,count(*) as num FROM testTable ORDER BY num desc

去重选择后返回条数

SELECT COUNT(DISTINCT(id)) from 数据表 WHERE createdDate BETWEEN '2009-08-06 15:53:14' AND '2009-09-01 15:53:14'

查出第几周到第几周的区间数据:

SELECT COUNT(DISTINCT(id)) ,WEEK(date_format('2009-08-05 15:53:14','%Y-%m-%d'),1) AS startweek,WEEK(date_format('2009-08-06 15:53:14','%Y-%m-%d'),1) AS endweekfrom 数据表WHERE WEEK(date_format(createdDate,'%Y-%m-%d'),1)BETWEEN WEEK(date_format('2009-08-05 15:53:14','%Y-%m-%d'),1) AND WEEK(date_format('2009-08-06 15:53:14','%Y-%m-%d'),1);

多表关联查询:

SELECTCOUNT(DISTINCT(account)) as fuNum, op.*,p.*from A oLEFT JOIN B opON o.id = op.idLEFT JOIN C pON op.id = p.idWHERE WEEK(date_format(createdDate,'%Y-%m-%d'),1)BETWEEN WEEK(date_format('2009-08-05 15:53:14','%Y-%m-%d'),1) AND WEEK(date_format('2009-08-06 15:53:14','%Y-%m-%d'),1)AND id = 3

函数查询求和:

SELECTCOUNT(DISTINCT(op.a)) as pa, COUNT(p.b) AS sa,SUM(o.c) AS tafrom A oLEFT JOIN B opON o.id = op.idLEFT JOIN C pON op.id = p.idWHERE WEEK(date_format(createdDate,'%Y-%m-%d'),1)BETWEEN WEEK(date_format('2009-08-05 15:53:14','%Y-%m-%d'),1) AND WEEK(date_format('2009-08-06 15:53:14','%Y-%m-%d'),1)AND id = 3

嵌套查询:

SELECTDISTINCT(CASE  accountWHEN '0' THEN 'null'ELSE (SELECT COUNT( DISTINCT(account) ) FROM  eps_order WHERE payStatus = 'paid') END) AS payNum,COUNT(p.`order`) AS saleOrders,SUM(o.amount) AS totalAmountfrom A oLEFT JOIN B opON o.id = op.orderIDLEFT JOIN C pON op.productID = p.idWHERE WEEK(date_format(createdDate,'%Y-%m-%d'),1)BETWEEN WEEK(date_format('2009-08-05 15:53:14','%Y-%m-%d'),1) AND WEEK(date_format('2009-08-06 15:53:14','%Y-%m-%d'),1)AND  o.`status` != 'canceled'AND productID = 3

多重嵌套查询:

SELECTDISTINCT(CASE  o.accountWHEN NULL THEN 'null'ELSE (SELECT COUNT( DISTINCT(o.account) ) from A oLEFT JOIN B opON o.id = op.orderIDLEFT JOIN C pON op.productID = p.idWHERE WEEK(date_format(createdDate,'%Y-%m-%d'),1)BETWEEN WEEK(date_format('2009-08-05 15:53:14','%Y-%m-%d'),1) AND WEEK(date_format('2009-08-06 15:53:14','%Y-%m-%d'),1)AND  o.`status` != 'canceled'AND productID = 3) END) AS payNum,COUNT(p.`order`) AS saleOrders,SUM(o.amount) AS totalAmountfrom A oLEFT JOIN B opON o.id = op.orderIDLEFT JOIN C pON op.productID = p.idWHERE WEEK(date_format(createdDate,'%Y-%m-%d'),1)BETWEEN WEEK(date_format('2018-08-05 15:53:14','%Y-%m-%d'),1) AND WEEK(date_format('2018-08-06 15:53:14','%Y-%m-%d'),1)AND  o.`status` != 'canceled'AND productID = 3

转载于:https://my.oschina.net/u/3632227/blog/3003643

你可能感兴趣的文章
vim目录说明
查看>>
说说css3布局
查看>>
输入 URL 到页面完成加载过程中的所有发生的事情?
查看>>
BZOJ 1004 HNOI2008 Cards Burnside引理
查看>>
android获取mp4视频文件总时长和视频宽高<转>
查看>>
ASP.NET 短路由配置
查看>>
教程-(SQL DBE、ADO连接)+(Firebird火鸟+DbExpress)+(VF DBF数据库)+(DB Paradox)
查看>>
php获胜的算法的概率,它可用于刮,大转盘等彩票的算法
查看>>
C语言函数指针
查看>>
Androidproject夹
查看>>
YUV编码格式
查看>>
python 安装easy_install和pip
查看>>
Open DJ备份与恢复方案
查看>>
Oracle创建用户设置权限
查看>>
当大数据遭遇深不可测的人性
查看>>
报错:tr was not declared in this scope
查看>>
Java反射
查看>>
批量扫描互联网无线路由设备telnet,并获取WIFI密码
查看>>
ubuntu中eclipse无法识别android手机问题
查看>>
HDU 4978 A simple probability problem
查看>>