日期計算 MySQL提供了幾個函數, 可以用來計算日期。巴拉拉拉。。。。 舉個例子:我想要確定pet們有多大, 可以計算當前日期的年和出生日期之間的差。 操作如下: SELECT name, birth, YEAR(CURRENT_DATE()) - YEAR(birth) AS age FROM ...
日期計算
MySQL提供了幾個函數, 可以用來計算日期。巴拉拉拉。。。。
舉個例子:我想要確定pet們有多大, 可以計算當前日期的年和出生日期之間的差。
操作如下:
SELECT name, birth, YEAR(CURRENT_DATE()) - YEAR(birth) AS age FROM pet;
mysql> SELECT name, birth, YEAR(CURRENT_DATE()) - YEAR(birth) AS age FROM pet;
+----------+------------+------+
| name | birth | age |
+----------+------------+------+
| Whistler | 1997-12-09 | 20 |
| Puffball | 1999-03-30 | 18 |
| Fluffy | 1993-02-04 | 24 |
| Claws | 1994-03-17 | 23 |
| Buffy | 1989-05-13 | 28 |
| Fang | 1990-08-27 | 27 |
| Bowser | 1989-08-31 | 28 |
| Chirpy | 1998-09-11 | 19 |
| Slim | 1996-04-29 | 21 |
+----------+------------+------+
9 rows in set (0.00 sec)
mysql>
YEAR() 取得是年份,沒毛病。
插一個千年蟲問題
溫馨提示:這裡很有可能遇到一個很經典的問題,叫千年蟲問題。
SELECT YEAR('89-01-01');這個處理得到的是1989
mysql> select year('89-01-01');
+------------------+
| year('89-01-01') |
+------------------+
| 1989 |
+------------------+
1 row in set (0.00 sec)
mysql>
SELECT YEAR('03-01-01');這個得到的是2003
mysql> SELECT YEAR('03-01-01');
+------------------+
| YEAR('03-01-01') |
+------------------+
| 2003 |
+------------------+
1 row in set (0.00 sec)
mysql>
還有上古程式員跟我說個69 70 是個坎我試了一下
mysql> select year('69-01-01');
+------------------+
| year('69-01-01') |
+------------------+
| 2069 |
+------------------+
1 row in set (0.00 sec)
mysql> select year('70-01-01');
+------------------+
| year('70-01-01') |
+------------------+
| 1970 |
+------------------+
1 row in set (0.00 sec)
mysql>
還真是。我的想法是電腦世界的第一天是:1970-01-01嘛。所以就有了這麼一個坎。現在Java是可以到達1970-01-01之前的天數的,在這裡不多說了。
好,書歸正傳。剛纔我們給寵物們計算出了age ,然而一個懂Mysql的客戶看到了很不滿意。寵物的年齡應該按照過了生日計算(生下來算0歲)。
操作如下:
SELECT name, birth, CURDATE(), (YEAR(CURDATE()) - YEAR(birth)) - (RIGHT(CURDATE(), 5) < RIGHT(birth, 5)) AS age FROM pet;
mysql> SELECT name, birth, CURDATE(),
-> (YEAR(CURDATE()) - YEAR(birth)) -
-> (RIGHT(CURDATE(), 5) < RIGHT(birth, 5)) AS age
-> FROM pet;
+----------+------------+------------+------+
| name | birth | CURDATE() | age |
+----------+------------+------------+------+
| Whistler | 1997-12-09 | 2017-05-26 | 19 |
| Puffball | 1999-03-30 | 2017-05-26 | 18 |
| Fluffy | 1993-02-04 | 2017-05-26 | 24 |
| Claws | 1994-03-17 | 2017-05-26 | 23 |
| Buffy | 1989-05-13 | 2017-05-26 | 28 |
| Fang | 1990-08-27 | 2017-05-26 | 26 |
| Bowser | 1989-08-31 | 2017-05-26 | 27 |
| Chirpy | 1998-09-11 | 2017-05-26 | 18 |
| Slim | 1996-04-29 | 2017-05-26 | 21 |
+----------+------------+------------+------+
9 rows in set (0.00 sec)
mysql>
RIGHT(birth, 5)就是取到了生日的日期。
我這裡又有一個大膽的想法:Mysql是C/C++開發的嘛 我就猜Mysql的運算中bool類型也是可以和int運算的。
又是一個慄子:其實還有一個漏洞,你沒有註意到。有一個pet已經死了。死了怎麼可能還有age呢?我們先查一下死去的pet有哪些。
操作如下:
SELECT name, birth, death,(YEAR(death)-YEAR(birth)) - (RIGHT(death,5) < RIGHT(birth,5)) AS age FROM pet WHERE death IS NOT NULL;
mysql> SELECT name,birth,death,
-> (YEAR(death)-YEAR(birth)) -
-> (RIGHT(death,5)<RIGHT(birth,5)) AS age
-> FROM pet
-> WHERE death IS NOT NULL;
+--------+------------+------------+------+
| name | birth | death | age |
+--------+------------+------------+------+
| Bowser | 1989-08-31 | 1995-07-29 | 5 |
+--------+------------+------------+------+
1 row in set (0.00 sec)
mysql>
註意:NULL是特殊的值, 不能用普通的比較符比較
mysql> SELECT name,birth,death,
-> (YEAR(death)-YEAR(birth)) -
-> (RIGHT(death,5)<RIGHT(birth,5)) AS age
-> FROM pet
-> WHERE death != NULL;
Empty set (0.00 sec)
mysql>
剛纔說了那麼多關於YEAR()的日期提取函數, 其實還有很多 比如說 MONTH() 和 DAYOFMONTH() 這兩個;
操作如下:
SELECT name, birth, MONTH(birth), DAYOFMONTH(birth) FROM pet ORDER BY species DESC;
mysql> SELECT name, birth, MONTH(birth), DAYOFMONTH(birth)
-> FROM pet ORDER BY species DESC;
+----------+------------+--------------+-------------------+
| name | birth | MONTH(birth) | DAYOFMONTH(birth) |
+----------+------------+--------------+-------------------+
| Slim | 1996-04-29 | 4 | 29 |
| Puffball | 1999-03-30 | 3 | 30 |
| Buffy | 1989-05-13 | 5 | 13 |
| Fang | 1990-08-27 | 8 | 27 |
| Bowser | 1989-08-31 | 8 | 31 |
| Fluffy | 1993-02-04 | 2 | 4 |
| Claws | 1994-03-17 | 3 | 17 |
| Whistler | 1997-12-09 | 12 | 9 |
| Chirpy | 1998-09-11 | 9 | 11 |
+----------+------------+--------------+-------------------+
9 rows in set (0.00 sec)
mysql>
接著往後面玩:假定當前月是4月(其實是五月),我要提前一個月準備給pet們過生日。So,我應該查找5月份過生日的pets。
操作如下:
SELECT name, birth FROM pet WHERE MONTH(birth) = 5;
mysql> SELECT name, birth
-> FROM pet
-> WHERE MONTH(birth) = 5;
+-------+------------+
| name | birth |
+-------+------------+
| Buffy | 1989-05-13 |
+-------+------------+
1 row in set (0.22 sec)
mysql>
這時候,看喜歡套路的你肯定會知道。問題難度要小小升級一下。
現在我不知道現在是幾月份,我就是想要小個月準備給pets過生日。What should I do ?
操作如下:
SELECT name, birth FROM pet WHERE MONTH(birth) = MONTH(CURDATE()) + 1;
然而天天日代碼的老司機們都知道!上面的操作一定是假的。這是有問題的!
當我要給一月份的pets準備生日我用上面的語句能查到嗎?查不到,根本就沒有0月!
那經驗豐富的程式員們一定在心中有一個大膽的想法:Mysql中是不是有'%'啊?
沒錯,是有的。
操作如下:
SELECT name, birth FROM pet WHERE MONTH(birth) = MOD(MONTH(CURDATE()), 12) + 1;
mysql> SELECT name,birth
-> FROM pet
-> WHERE MONTH(birth) = MOD(MONTH(CURDATE()), 12) + 1;
Empty set (0.00 sec)
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2017-05-26 16:01:09 |
+---------------------+
1 row in set (0.04 sec)
mysql>
...這TM就尷尬了,沒有在6月份過生日的pet.
沒關係,我們的套路繼續(書上的套路繼續)。下麵給出書上的另一種方法。
操作如下:
SELECT name, birth FROM pet WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(), INTERVAL 1 MONTH));
mysql> SELECT name, birth FROM pet
-> WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(), INTERVAL 1 MONTH));
Empty set (0.10 sec)
mysql>
這裡用到了一個DATE_ADD()函數,具體的講解給大家一個傳送門。如果傳送門失效了,那就去百度吧騷年。
to be continued...