关于日期处理的实例:

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

然后执行下面的操作:

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

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

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

接下来是为了计算今年和明年的生日,注意如果生日为2月29日,且目标日期不是闰月,那么这两列所包含的将是2月28日,而非3月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;
+-------------------+------------+---------------------+------------+------------+
| name | birthday | today | cur | next |
+-------------------+------------+---------------------+------------+------------+
| FacelloGeorgi | 1953-09-02 | 2013-12-08 02:14:27 | 2013-09-02 | 2014-09-02 |
| SimmelBezalel | 1964-06-02 | 2013-12-08 02:14:27 | 2013-06-02 | 2014-06-02 |
| BamfordParto | 1959-12-03 | 2013-12-08 02:14:27 | 2013-12-03 | 2014-12-03 |
| KoblickChirstian | 1954-05-01 | 2013-12-08 02:14:27 | 2013-05-01 | 2014-05-01 |
| MaliniakKyoichi | 1955-01-21 | 2013-12-08 02:14:27 | 2013-01-21 | 2014-01-21 |
| PreusigAnneke | 1953-04-20 | 2013-12-08 02:14:27 | 2013-04-20 | 2014-04-20 |
| ZielinskiTzvetan | 1957-05-23 | 2013-12-08 02:14:27 | 2013-05-23 | 2014-05-23 |
| KalloufiSaniya | 1958-02-19 | 2013-12-08 02:14:27 | 2013-02-19 | 2014-02-19 |
| PeacSumant | 1952-04-19 | 2013-12-08 02:14:27 | 2013-04-19 | 2014-04-19 |
| PiveteauDuangkaew | 1963-06-01 | 2013-12-08 02:14:27 | 2013-06-01 | 2014-06-01 |
| amosli | 1972-02-29 | 2013-12-08 02:14:27 | 2013-02-28 | 2014-02-28 |
+-------------------+------------+---------------------+------------+------------+
11 rows in set (0.00 sec)

如果出生的日期是闰月,并且当前的年份不是闰年,那么日期加一,表示3月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; 
+-------------------+------------+------------+---------------------+------------+
| name | birthday | next | today | cur |
+-------------------+------------+------------+---------------------+------------+
| FacelloGeorgi | 1953-09-02 | 2014-09-02 | 2013-12-08 02:19:07 | 2013-09-02 |
| SimmelBezalel | 1964-06-02 | 2014-06-02 | 2013-12-08 02:19:07 | 2013-06-02 |
| BamfordParto | 1959-12-03 | 2014-12-03 | 2013-12-08 02:19:07 | 2013-12-03 |
| KoblickChirstian | 1954-05-01 | 2014-05-01 | 2013-12-08 02:19:07 | 2013-05-01 |
| MaliniakKyoichi | 1955-01-21 | 2014-01-21 | 2013-12-08 02:19:07 | 2013-01-21 |
| PreusigAnneke | 1953-04-20 | 2014-04-20 | 2013-12-08 02:19:07 | 2013-04-20 |
| ZielinskiTzvetan | 1957-05-23 | 2014-05-23 | 2013-12-08 02:19:07 | 2013-05-23 |
| KalloufiSaniya | 1958-02-19 | 2014-02-19 | 2013-12-08 02:19:07 | 2013-02-19 |
| PeacSumant | 1952-04-19 | 2014-04-19 | 2013-12-08 02:19:07 | 2013-04-19 |
| PiveteauDuangkaew | 1963-06-01 | 2014-06-01 | 2013-12-08 02:19:07 | 2013-06-01 |
| amosli | 1972-02-29 | 2014-03-01 | 2013-12-08 02:19:07 | 2013-03-01 |
+-------------------+------------+------------+---------------------+------------+
11 rows in set (0.00 sec)

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

 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;
+-------------------+------------+------------+
| name | birthday | birth_day |
+-------------------+------------+------------+
| FacelloGeorgi | 1953-09-02 | 2014-09-02 |
| SimmelBezalel | 1964-06-02 | 2014-06-02 |
| BamfordParto | 1959-12-03 | 2014-12-03 |
| KoblickChirstian | 1954-05-01 | 2014-05-01 |
| MaliniakKyoichi | 1955-01-21 | 2014-01-21 |
| PreusigAnneke | 1953-04-20 | 2014-04-20 |
| ZielinskiTzvetan | 1957-05-23 | 2014-05-23 |
| KalloufiSaniya | 1958-02-19 | 2014-02-19 |
| PeacSumant | 1952-04-19 | 2014-04-19 |
| PiveteauDuangkaew | 1963-06-01 | 2014-06-01 |
| amosli | 1972-02-29 | 2014-03-01 |
+-------------------+------------+------------+
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. [VS2010搭建汇编开发环境win32和x64]

    场景: 1. 虽然使用MASM32也可以编译运行汇编程序,但是既然装了VS2010,它也能支持编译运行汇编吧.不然微软的开发人员难道还不用vs写汇编程序了? http://www.masm32.com ...

  2. SAP ABAP编程 取得用户中文名称

    有时候我们知道SAP当前用户登录的ID,也就是SY-UNAME.能够取得用户中文名称.例如以下: ***取得用户中文名称 DATA: g_sheet_jsr TYPE string.  "用 ...

  3. Android教你怎样一步步打造通用适配器

    前言 在Android开发中ListView是最为经常使用的控件之中的一个,基本每一个应用都会涉及到它,要使用ListView列表展示,就不可避免地涉及到另外一个东西--Adapter,我们都知道,A ...

  4. Ubuntu环境变量解析

    在Ubuntu中有如下几个文件可以设置环境变量 /etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. /e ...

  5. Android -- startActivityForResult和setResult

    startActivityForResult与startActivity的不同之处 startActivity( ) 仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startActivit ...

  6. Tushare数据的绘图操作

    1.在代码里调试学习实在费劲,可以把数据取到df里,在交互界面里慢慢调试 2.柱状图 绘制柱状图,默认情况下乱,数据太密了 改用曲线图

  7. [Node.js] process.nextTick for converting sync to async

    For example we have a function to check the filesize: const fs = require('fs'); function fileSize (f ...

  8. DOM元素尺寸offsetWidth,scrollWidth,clientWidth等具体解释

    样例: <div id="div" style="height: 200px;width: 200px;border:solid 50px red;overflow ...

  9. 牛客网-《剑指offer》-二进制中1的个数

    题目:http://www.nowcoder.com/practice/8ee967e43c2c4ec193b040ea7fbb10b8 C++ 负数需要特殊处理,因为负数右移会补1(符号位) cla ...

  10. LNMP一键安装包-CentOS/Ubuntu/Debian自动安装Nginx,MySQL,PHP

    适用环境: 系统支持:CentOS.Ubuntu.Debian 内存要求:≥128M 安装了什么: 1.Nginx-1.2.1 2.MySQL 5.5.25 3.PHP 5.2.17或PHP 5.3. ...