Mysql查询时间区间日期列表,不会由于数据表数据影响


Posted in MySQL onApril 19, 2022

1、查询时间区间日期列表,不会由于数据表数据影响

select a.date 
from (
    select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a) ) DAY as date
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as d
) a
where a.date between '2020-01-20' and '2021-12-24' ORDER BY a.date asc

 tips:如果要查询当前日期后面的数据 curdate()改为截止日期就好

2、创建视图可以公共使用

CREATE VIEW v_digits AS
  SELECT 0 AS digit UNION ALL
  SELECT 1 UNION ALL
  SELECT 2 UNION ALL
  SELECT 3 UNION ALL
  SELECT 4 UNION ALL
  SELECT 5 UNION ALL
  SELECT 6 UNION ALL
  SELECT 7 UNION ALL
  SELECT 8 UNION ALL
  SELECT 9;
 
CREATE VIEW v_numbers AS
  SELECT
    ones.digit + tens.digit * 10 + hundreds.digit * 100 + thousands.digit * 1000 AS number
  FROM
    v_digits as ones,
    v_digits as tens,
    v_digits as hundreds,
    v_digits as thousands;
		
-- 生成的日期格式为  yyyy-MM-dd		
CREATE VIEW v_dates AS
  SELECT
    SUBDATE(CURRENT_DATE(), number) AS date
  FROM
    v_numbers
  UNION ALL
  SELECT
    ADDDATE(CURRENT_DATE(), number + 1) AS date
  FROM
    v_numbers;
 
-- 生成的日期格式为 yyyy-MM
CREATE VIEW v_months AS
  SELECT
    DATE_FORMAT(SUBDATE(CURRENT_DATE(), INTERVAL number MONTH),'%Y-%m')  AS date
  FROM
    v_numbers
  UNION ALL
  SELECT
    DATE_FORMAT(ADDDATE(CURRENT_DATE(), INTERVAL number+1 MONTH),"%Y-%m") AS date
  FROM
    v_numbers;

Mysql查询时间区间日期列表,不会由于数据表数据影响

3、创建为视图之后,可以通过视图查询时间区间列表日期

SELECT
  date
FROM
  v_dates
WHERE
  date BETWEEN '2020-01-20' AND '2021-01-24'
ORDER BY
  date asc

Mysql查询时间区间日期列表,不会由于数据表数据影响

4、查询时间区间按月

select DATE_FORMAT(str_to_date (a.Date,'%Y-%m-%d'),"%Y-%m") as Date 
from (
    select '2011-12-24' - INTERVAL (a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a) ) MONTH as Date
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as d
) a
where a.Date between '2010-01-20' and '2011-12-24' ORDER BY a.Date asc;

Mysql查询时间区间日期列表,不会由于数据表数据影响

附:在对mysql的时间进行区间查询的时候出现的问题

<if test="searchcondition.starttime!=null">
    <![CDATA[ and select_data.data_time  >= #{searchcondition.starttime,jdbcType=TIMESTAMP} ]]>
</if>

<if test="searchcondition.stoptime!=null">
    <![CDATA[ and select_data.data_time <= #{searchcondition.stoptime,jdbcType=TIMESTAMP} ]]>
</if>

在test中不能使用searchcondition.stoptime!=’ ‘这个判断会报错,上面的是标准的时间查询,自己做的时候总是会加上!=’ ‘这个条件.所以总是报错,记录一下.

总结

到此这篇关于Mysql查询时间区间日期列表的文章就介绍到这了!

MySQL 相关文章推荐
多表查询、事务、DCL
Apr 05 MySQL
MySQL官方导出工具mysqlpump的使用
May 21 MySQL
Mysql 用户权限管理实现
May 25 MySQL
Mysql中 unique列插入重复值该怎么解决呢
May 26 MySQL
MySQL中in和exists区别详解
Jun 03 MySQL
Prometheus 监控MySQL使用grafana展示
Aug 30 MySQL
mysql下的max_allowed_packet参数设置详解
Feb 12 MySQL
mysql自增长id用完了该怎么办
Feb 12 MySQL
MySQL磁盘碎片整理实例演示
Apr 03 MySQL
MySQL普通表如何转换成分区表
May 30 MySQL
MySQL下载安装配置详细教程 附下载资源
Sep 23 MySQL
mysql序号rownum行号实现方式
Dec 24 MySQL
WINDOWS下安装mysql 8.x 的方法图文教程
CentOS MySql8 远程连接实战
Mysql排查分析慢sql之explain实战案例
Apr 19 #MySQL
MySQL视图概念以及相关应用
mysql 乱码 字符集latin1转UTF8
Apr 19 #MySQL
MySQL 表锁定 LOCK和UNLOCK TABLES的 SQL语法
Apr 18 #MySQL
为什么MySQL8新特性会修改自增主键属性
You might like
php分页思路以及在ZF中的使用
2012/05/30 PHP
php批量上传的实现代码
2013/06/09 PHP
ThinkPHP利用PHPMailer实现邮件发送实现代码
2013/09/26 PHP
PHP开发注意事项总结
2015/02/04 PHP
php单例模式的简单实现方法
2016/06/10 PHP
基于jquery1.4.2的仿flash超炫焦点图播放效果
2010/04/20 Javascript
标题过长使用javascript按字节截取字符串
2014/04/24 Javascript
Linux下使用jq友好的打印JSON技巧分享
2014/11/18 Javascript
jQuery中data()方法用法实例
2014/12/27 Javascript
javascript实现动态导入js与css等静态资源文件的方法
2015/07/25 Javascript
JavaScript实现同一页面内两个表单互相传值的方法
2015/08/12 Javascript
实例讲解使用原生JavaScript处理AJAX请求的方法
2016/05/10 Javascript
JavaScript导航脚本判断当前导航
2016/07/12 Javascript
jquery.multiselect多选下拉框实现代码
2016/11/11 Javascript
微信小程序联网请求的轮播图
2017/07/07 Javascript
Js判断H5上下滑动方向及滑动到顶部和底部判断的示例代码
2017/11/15 Javascript
webuploader实现上传图片到服务器功能
2018/08/16 Javascript
微信小程序设置全局请求URL及封装wx.request请求操作示例
2019/04/02 Javascript
详解JS深拷贝与浅拷贝
2020/08/04 Javascript
vue+element table表格实现动态列筛选的示例代码
2021/01/14 Vue.js
Python配置文件解析模块ConfigParser使用实例
2015/04/13 Python
利用Python的Django框架中的ORM建立查询API
2015/04/20 Python
python遍历目录的方法小结
2016/04/28 Python
python3调用百度翻译API实现实时翻译
2018/08/16 Python
python提取包含关键字的整行数据方法
2018/12/11 Python
Pyqt5实现英文学习词典
2019/06/24 Python
python  ceiling divide 除法向上取整(或小数向上取整)的实例
2019/12/27 Python
用python介绍4种常用的单链表翻转的方法小结
2020/02/24 Python
对python中return与yield的区别详解
2020/03/12 Python
美国美发品牌:Bumble and Bumble
2016/10/08 全球购物
HealthElement海外旗舰店:新西兰大卖场
2018/02/23 全球购物
致垒球运动员加油稿
2014/02/16 职场文书
基层工作经历证明
2015/06/19 职场文书
纯CSS实现酷炫的霓虹灯效果
2021/04/13 HTML / CSS
Django如何与Ajax交互
2021/04/29 Python
Mybatis-Plus 使用 @TableField 自动填充日期
2022/04/26 Java/Android