博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL学习笔记:创建整年日期
阅读量:4963 次
发布时间:2019-06-12

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

  见识到另外一种创意,惊讶!

  1.创建小数据表 0-9

# 创建小数据表 0-9DROP TABLE IF EXISTS aa_numbers_small;CREATE TABLE aa_numbers_small(    number INT);INSERT INTO aa_numbers_small VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9);SELECT * FROM aa_numbers_small;

  2.创建大数据表 0-10000

# 创建大数据表 0-10000DROP TABLE IF EXISTS aa_numbers;CREATE TABLE aa_numbers(number BIGINT);INSERT INTO aa_numbersSELECT thousand.number*1000+hundred.number*100+ten.number*10+ones.numberFROM aa_numbers_small thousand, aa_numbers_small hundred, aa_numbers_small ten, aa_numbers_small onesLIMIT 1000000;SELECT * FROM aa_numbers;

  3.创建时间维表

# 创建时间维表DROP TABLE IF EXISTS aa_date;CREATE TABLE aa_date(    date_id          BIGINT PRIMARY KEY,    DATE             DATE NOT NULL,    DAY              CHAR(10),    day_of_week      INT,    day_of_month     INT,    day_of_year      INT,    previous_day     DATE NOT NULL DEFAULT '0000-00-00',    next_day         DATE NOT NULL DEFAULT '0000-00-00',    weekend          CHAR(10) NOT NULL DEFAULT "Weekday",    week_of_year     CHAR(2),    MONTH            CHAR(10),    month_of_year    CHAR(2),    quarter_of_year INT,    YEAR             INT,    UNIQUE KEY `date` (`date`));SELECT * FROM aa_date;

  4.填充数据

# 填充数据INSERT INTO aa_date(date_id, DATE)SELECT number, DATE_ADD('2018-01-01', INTERVAL number DAY)FROM aa_numbersWHERE DATE_ADD('2018-01-01', INTERVAL number DAY) BETWEEN '2018-01-01' AND '2018-12-31'ORDER BY number;

  5.根据日期update其他列数据

# 根据日期update其他列数据UPDATE aa_date SETDAY             = DATE_FORMAT( DATE, "%W" ),day_of_week     = DAYOFWEEK(DATE),day_of_month    = DATE_FORMAT( DATE, "%d" ),day_of_year     = DATE_FORMAT( DATE, "%j" ),previous_day    = DATE_ADD(DATE, INTERVAL -1 DAY),next_day        = DATE_ADD(DATE, INTERVAL 1 DAY),weekend         = IF( DATE_FORMAT( DATE, "%W" ) IN ('Saturday','Sunday'), 'Weekend', 'Weekday'),week_of_year    = DATE_FORMAT( DATE, "%V" ),MONTH           = DATE_FORMAT( DATE, "%M"),month_of_year   = DATE_FORMAT( DATE, "%m"),quarter_of_year = QUARTER(DATE),YEAR            = DATE_FORMAT( DATE, "%Y" );

  6.日期函数测试

# 测试SELECT DATE_FORMAT('2018-05-29', '%W'); # TuesdaySELECT DAYOFWEEK('2018-05-29'); # 3SELECT DATE_FORMAT('2018-05-29', '%d'); # 29SELECT DATE_FORMAT('2018-05-29', '%j'); # 149SELECT DATE_ADD('2018-05-29', INTERVAL -1 DAY); # 2018-05-28SELECT DATE_SUB('2018-05-29', INTERVAL 1 DAY); # 2018-05-28SELECT DATE_ADD('2018-05-29', INTERVAL 1 DAY); # 2018-05-30SELECT IF(DATE_FORMAT('2018-05-26', '%W') IN ('Saturday','Sunday'), 'Weekend', 'Weekday'); # WeekendSELECT DATE_FORMAT('2018-05-29', '%V'); # 21SELECT DATE_FORMAT('2018-05-29', '%M'); # MaySELECT DATE_FORMAT('2018-05-29', '%m'); # 05SELECT QUARTER('2018-05-29') # 2SELECT DATE_FORMAT('2018-05-29', '%Y');# 2018

END 2018-05-29 14:34:57 

转载于:https://www.cnblogs.com/hider/p/9104943.html

你可能感兴趣的文章
算法-翻转句子中单词的顺序
查看>>
关于测绘类软件的学习心得
查看>>
二叉搜索树的后序遍历序列 (java)
查看>>
Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
查看>>
微信小说域名被封-366tool在线解答微信屏蔽小说网页停止访问的解决方案
查看>>
.NET Core项目与传统vs项目的细微不同
查看>>
TouchDevelop [Mobile App]
查看>>
BZOJ 1861: [Zjoi2006]Book 书架
查看>>
矩阵乘法&&dp加速矩阵的思路(E. Wet Shark and Blocks)
查看>>
Netflix开源的Spring Cloud主机级性能监控框架--Vector
查看>>
HTML5 在泛在电力物联网的 10 大业务领域 2/3D 可视化应用
查看>>
共享Visio和project的下载链接
查看>>
windows mysql提示:1045 access denied for user'root'@'localhost' using password yes
查看>>
etcd 命令
查看>>
生产故障处理思路
查看>>
mysql的SQL_CALC_FOUND_ROWS 使用
查看>>
Quartz 2D编程指南(1) - 概览
查看>>
《Android深度探索卷一》读书笔记一
查看>>
【C语言】Coursera课程《计算机程式设计》台湾大学刘邦锋——Week2课堂笔记
查看>>
Hibernate JPA 如何使用SQL文直接查询
查看>>