关于日期处理的实例:

从mysql给出的 example 这个是官方源码下载以及导入,http://dev.mysql.com/doc/employee/en/employees-installation.html

然后执行下面的操作:

  1. mysql> create table employees like employees.employees;
  2. Query OK, 0 rows affected (0.11 sec)
  3.  
  4. mysql> desc employees;
  5. +------------+---------------+------+-----+---------+-------+
  6. | Field | Type | Null | Key | Default | Extra |
  7. +------------+---------------+------+-----+---------+-------+
  8. | emp_no | int(11) | NO | PRI | NULL | |
  9. | birth_date | date | NO | | NULL | |
  10. | first_name | varchar(14) | NO | | NULL | |
  11. | last_name | varchar(16) | NO | | NULL | |
  12. | gender | enum('M','F') | NO | | NULL | |
  13. | hire_date | date | NO | | NULL | |
  14. +------------+---------------+------+-----+---------+-------+
  15. 6 rows in set (0.00 sec)
  16. 从其他数据库中指定的表中导入数据,employees.employees 导入前10条数据
  17. mysql> insert into employees select * from employees.employees limit 10;
  18. Query OK, 10 rows affected (0.04 sec)
  19. Records: 10 Duplicates: 0 Warnings: 0
  20. 仅查询employess表中的last_name,first_namebirthd_date三个数据
  21. mysql> select concat(last_name,' ',first_name) as name,birth_date as birthday from employees;
  22. mysql> select concat(last_name,' ',first_name) as name,birth_date as birthday from employees;
  23. +--------------------+------------+
  24. | name | birthday |
  25. +--------------------+------------+
  26. | Facello Georgi | 1953-09-02 |
  27. | Simmel Bezalel | 1964-06-02 |
  28. | Bamford Parto | 1959-12-03 |
  29. | Koblick Chirstian | 1954-05-01 |
  30. | Maliniak Kyoichi | 1955-01-21 |
  31. | Preusig Anneke | 1953-04-20 |
  32. | Zielinski Tzvetan | 1957-05-23 |
  33. | Kalloufi Saniya | 1958-02-19 |
  34. | Peac Sumant | 1952-04-19 |
  35. | Piveteau Duangkaew | 1963-06-01 |
  36. | amos li | 1972-02-29 |
  37. +--------------------+------------+
  38. 11 rows in set (0.00 sec)

然后执行下面的命令:用来计算每位员工的出生日期与当前日期相差的年份,以及当前的日期.

  1. select concat(last_name,first_name) as name,birth_date as birthday,(YEAR(now())-YEAR(birth_date)) as diff, now() as today from employees;
  1. +-------------------+------------+------+---------------------+
  2. | name | birthday | diff | today |
  3. +-------------------+------------+------+---------------------+
  4. | FacelloGeorgi | 1953-09-02 | 60 | 2013-12-08 02:12:54 |
  5. | SimmelBezalel | 1964-06-02 | 49 | 2013-12-08 02:12:54 |
  6. | BamfordParto | 1959-12-03 | 54 | 2013-12-08 02:12:54 |
  7. | KoblickChirstian | 1954-05-01 | 59 | 2013-12-08 02:12:54 |
  8. | MaliniakKyoichi | 1955-01-21 | 58 | 2013-12-08 02:12:54 |
  9. | PreusigAnneke | 1953-04-20 | 60 | 2013-12-08 02:12:54 |
  10. | ZielinskiTzvetan | 1957-05-23 | 56 | 2013-12-08 02:12:54 |
  11. | KalloufiSaniya | 1958-02-19 | 55 | 2013-12-08 02:12:54 |
  12. | PeacSumant | 1952-04-19 | 61 | 2013-12-08 02:12:54 |
  13. | PiveteauDuangkaew | 1963-06-01 | 50 | 2013-12-08 02:12:54 |
  14. | amosli | 1972-02-29 | 41 | 2013-12-08 02:12:54 |
  15. +-------------------+------------+------+---------------------+
  16. 11 rows in set (0.00 sec)

接下来是为了计算今年和明年的生日,注意如果生日为2月29日,且目标日期不是闰月,那么这两列所包含的将是2月28日,而非3月1日.

  1. select name,birthday,today,date_add(birthday,interval diff year) as cur,date_add(birthday,interval diff+1 year ) as next from (select concat(last_name,first_name) as name,birth_date as birthday,(YEAR(now())-YEAR(birth_date)) as diff, now() as today from employees) as a;
  1. +-------------------+------------+---------------------+------------+------------+
  2. | name | birthday | today | cur | next |
  3. +-------------------+------------+---------------------+------------+------------+
  4. | FacelloGeorgi | 1953-09-02 | 2013-12-08 02:14:27 | 2013-09-02 | 2014-09-02 |
  5. | SimmelBezalel | 1964-06-02 | 2013-12-08 02:14:27 | 2013-06-02 | 2014-06-02 |
  6. | BamfordParto | 1959-12-03 | 2013-12-08 02:14:27 | 2013-12-03 | 2014-12-03 |
  7. | KoblickChirstian | 1954-05-01 | 2013-12-08 02:14:27 | 2013-05-01 | 2014-05-01 |
  8. | MaliniakKyoichi | 1955-01-21 | 2013-12-08 02:14:27 | 2013-01-21 | 2014-01-21 |
  9. | PreusigAnneke | 1953-04-20 | 2013-12-08 02:14:27 | 2013-04-20 | 2014-04-20 |
  10. | ZielinskiTzvetan | 1957-05-23 | 2013-12-08 02:14:27 | 2013-05-23 | 2014-05-23 |
  11. | KalloufiSaniya | 1958-02-19 | 2013-12-08 02:14:27 | 2013-02-19 | 2014-02-19 |
  12. | PeacSumant | 1952-04-19 | 2013-12-08 02:14:27 | 2013-04-19 | 2014-04-19 |
  13. | PiveteauDuangkaew | 1963-06-01 | 2013-12-08 02:14:27 | 2013-06-01 | 2014-06-01 |
  14. | amosli | 1972-02-29 | 2013-12-08 02:14:27 | 2013-02-28 | 2014-02-28 |
  15. +-------------------+------------+---------------------+------------+------------+
  16. 11 rows in set (0.00 sec)

如果出生的日期是闰月,并且当前的年份不是闰年,那么日期加一,表示3月1日为生日,对于下一个年份使用同样的操作:

  1. select name,birthday,date_add(next,interval if(day(birthday)=29&&day(next)=28,1,0) day) as next,today,date_add(cur,interval if(day(birthday)=29&&day(cur)=28,1,0) day) as cur from(select name,birthday,today,date_add(birthday,interval diff year) as cur,date_add(birthday,interval diff+1 year ) as next from (select concat(last_name,first_name) as name,birth_date as birthday,(YEAR(now())-YEAR(birth_date)) as diff, now() as today from employees) as a) as b;
  1. +-------------------+------------+------------+---------------------+------------+
  2. | name | birthday | next | today | cur |
  3. +-------------------+------------+------------+---------------------+------------+
  4. | FacelloGeorgi | 1953-09-02 | 2014-09-02 | 2013-12-08 02:19:07 | 2013-09-02 |
  5. | SimmelBezalel | 1964-06-02 | 2014-06-02 | 2013-12-08 02:19:07 | 2013-06-02 |
  6. | BamfordParto | 1959-12-03 | 2014-12-03 | 2013-12-08 02:19:07 | 2013-12-03 |
  7. | KoblickChirstian | 1954-05-01 | 2014-05-01 | 2013-12-08 02:19:07 | 2013-05-01 |
  8. | MaliniakKyoichi | 1955-01-21 | 2014-01-21 | 2013-12-08 02:19:07 | 2013-01-21 |
  9. | PreusigAnneke | 1953-04-20 | 2014-04-20 | 2013-12-08 02:19:07 | 2013-04-20 |
  10. | ZielinskiTzvetan | 1957-05-23 | 2014-05-23 | 2013-12-08 02:19:07 | 2013-05-23 |
  11. | KalloufiSaniya | 1958-02-19 | 2014-02-19 | 2013-12-08 02:19:07 | 2013-02-19 |
  12. | PeacSumant | 1952-04-19 | 2014-04-19 | 2013-12-08 02:19:07 | 2013-04-19 |
  13. | PiveteauDuangkaew | 1963-06-01 | 2014-06-01 | 2013-12-08 02:19:07 | 2013-06-01 |
  14. | amosli | 1972-02-29 | 2014-03-01 | 2013-12-08 02:19:07 | 2013-03-01 |
  15. +-------------------+------------+------------+---------------------+------------+
  16. 11 rows in set (0.00 sec)

最后判断今年的生日是否已经过了,如果是,那么返回下一年的生日,最后得到的查询结果如下所示:

  1. select name,birthday,if(cur>today,cur,next) as birth_day from(select name,birthday,date_add(next,interval if(day(birthday)=29&&day(next)=28,1,0) day) as next,today,date_add(cur,interval if(day(birthday)=29&&day(cur)=28,1,0) day) as cur from(select name,birthday,today,date_add(birthday,interval diff year) as cur,date_add(birthday,interval diff+1 year ) as next from (select concat(last_name,first_name) as name,birth_date as birthday,(YEAR(now())-YEAR(birth_date)) as diff, now() as today from employees) as a) as b) as c;
  1. +-------------------+------------+------------+
  2. | name | birthday | birth_day |
  3. +-------------------+------------+------------+
  4. | FacelloGeorgi | 1953-09-02 | 2014-09-02 |
  5. | SimmelBezalel | 1964-06-02 | 2014-06-02 |
  6. | BamfordParto | 1959-12-03 | 2014-12-03 |
  7. | KoblickChirstian | 1954-05-01 | 2014-05-01 |
  8. | MaliniakKyoichi | 1955-01-21 | 2014-01-21 |
  9. | PreusigAnneke | 1953-04-20 | 2014-04-20 |
  10. | ZielinskiTzvetan | 1957-05-23 | 2014-05-23 |
  11. | KalloufiSaniya | 1958-02-19 | 2014-02-19 |
  12. | PeacSumant | 1952-04-19 | 2014-04-19 |
  13. | PiveteauDuangkaew | 1963-06-01 | 2014-06-01 |
  14. | amosli | 1972-02-29 | 2014-03-01 |
  15. +-------------------+------------+------------+
  16. 11 rows in set (0.00 sec)

mysql--SQL编程(关于mysql中的日期,实例,判断生日是否为闰年) 学习笔记2.1的更多相关文章

  1. 《果壳中的C# C# 5.0 权威指南》 - 学习笔记

    <果壳中的C# C# 5.0 权威指南> ========== ========== ==========[作者] (美) Joseph Albahari (美) Ben Albahari ...

  2. vue中添加Echarts图表的使用,Echarts的学习笔记

    项目中需要使用一些折线图.柱状图.饼状图等等,之前使用过heightCharts(关于heightCharts请看我的另一篇 http://www.cnblogs.com/jasonwang2y60/ ...

  3. js面向对象编程:if中可以使用那些作为判断条件呢?

    作者来源http://www.2cto.com/kf/201407/314978.html搬运 在所有编程语言中if是最长用的判断之一,但在js中到底哪些东西可以在if中式作为判断表达式呢? 例如如何 ...

  4. 《C++编程规范:101条规则、准则与最佳实践》学习笔记

    转载:http://dsqiu.iteye.com/blog/1688217 组织和策略问题 0. 不要为小事斤斤计较.(或者说是:知道什么东西不需要标准化) 无需在多个项目或者整个公司范围内强制实施 ...

  5. vue.js中的各种问题记录(包括环境问题和学习笔记)

    一.this relative module was not found: 问题的意思是这个模块找不到了 解决方法: 1)查看你入口文件的路径是否写错: 2)查看360杀毒是否拦截了你的文件. 二.v ...

  6. SOC中的DFT和BIST对比与比较-IC学习笔记(二)

    ATE:ATE是Automatic Test Equipment的缩写,根据客户的测试要求.图纸及参考方案,采用MCU.PLC.PC基于VB.VC开发平台,利用TestStand&LabVIE ...

  7. Android日期时间选择器DatePicker、TimePicker日期时间改变事件响应(Android学习笔记)

    activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...

  8. python编程:从入门到实践--项目1-外星人入侵_学习笔记_源码

    这里有九个.py文件,在工作的间隙,和老板斗智斗勇,终于完成了,实现了游戏的功能,恰逢博客园开通,虽然是对着书上的代码敲了一遍,但是对pygam这个库的了解增加了一些,作为一个python初学者,也作 ...

  9. Java中关于 ArrayList 和 Map 的常用遍历方法 (学习笔记,便于以后查询)

    一.学习ArrayList与Map时,关于常用遍历方法的记录如下:  二.附源码如下: package com.study.in.myself; import java.util.ArrayList; ...

随机推荐

  1. 论述Android通过HttpURLConnection与HttpClient联网代理网关设置

    Android联网主要使用HttpURLConneciton和HttpClient进行联网,在手机联网的时候,我们优先选择wifi网络,其次在选择移动网络,这里所述移动网络主要指cmwap. 大家都知 ...

  2. nginx不浏览直接下载文件

    当我们使用Nginx时,如果要让一些附件比如txt,pdf,doc等不直接在浏览器打开,而弹出另存为的对话框(也就是下载),则可以在nginx里添加如下配置: location /{if ($requ ...

  3. 转:查看linux系统版本号

    转自: http://blog.csdn.net/zhuying_linux/article/details/6859286 lsb_release -a

  4. Intellij idea断点 Debugger slow: Method breakpoints my dramatically slow down debugging

    不知道点到哪里了,IDEA调试特别卡,而且总是如下提示, Debugger slow: Method breakpoints my dramatically slow down debugging 意 ...

  5. Robotframework(1):配置基础环境

    转载:http://www.cnblogs.com/CCGGAAG/p/7800324.html 最近对Robotframework这款基于python的框架比较感兴趣,于是想着来研究一下,下面我们来 ...

  6. docker toolbox在win7下的安装

    1.下载安装docker toolbox docker toolbox的下载地址: http://mirrors.aliyun.com/docker-toolbox/windows/docker-to ...

  7. 【Linux】Ubuntu配置服务自启动 sysv-rc-conf

    在Ubuntu下,配置服务系统开机自启动,使用的不是chkconfig,而是sysv-rc-conf. 且看如下: 安装: sudo apt-get install sysv-rc-conf 帮助信息 ...

  8. 带你走进EJB--将EJB发布为Webservice(3)

    在上面文章中我们讲到,通过使用用JBoss5作为EJB容器的时候,调用Web服务出现了异常. 异常信息如下: *********************** CreateWeb Service Cli ...

  9. mySQL内存及虚拟内存优化设置[转]

    mySQL内存及虚拟内存优化设置 . 数据库mySQL内存优化G-LB  为了装mysql环境测试,装上后发现启动后mysql占用了很大的虚拟内存,达8百多兆.网上搜索了一下,得到高人指点my.ini ...

  10. SHELL异常处理

    写SHELL好久了,经常被异常困扰,可竟然坚持了若干年没用过,回想以前服务过的公司,阿弥陀佛,罪过罪过.废话少说,希望此篇文章可以协助大家和我彻底结束SHELL脚本就是LINUX命令集合的初级阶段. ...