板凳-------Mysql cookbook學習 (十--5)

6.11 計算年齡 2025年6月11日星期三

--創建表、初始化數據
drop table if exists sibling;
create table sibling
(name  char(20),birth date
);insert into sibling (name,birth) values('Gretchen','1942-04-14');
insert into sibling (name,birth) values('Wilbur','1946-11-28');
insert into sibling (name,birth) values('Franz','1953-03-05');select * from sibling;
+----------+------------+
| name     | birth      |
+----------+------------+
| Gretchen | 1942-04-14 |
| Wilbur   | 1946-11-28 |
| Franz    | 1953-03-05 |
+----------+------------+
3 rows in set (0.00 sec)mysql> select name, birth, curdate() as today, timestampdiff(year, birth, curdate()) as 'age in years' from sibling;
+----------+------------+------------+--------------+
| name     | birth      | today      | age in years |
+----------+------------+------------+--------------+
| Gretchen | 1942-04-14 | 2025-06-11 |           83 |
| Wilbur   | 1946-11-28 | 2025-06-11 |           78 |
| Franz    | 1953-03-05 | 2025-06-11 |           72 |
+----------+------------+------------+--------------+
3 rows in set (0.00 sec)mysql> select name, birth, '1953-03-05' as 'Franz'' birth', timestampdiff(year, birth, '1953-03-05') as 'age in years' from sibling where name != 'Franz';
+----------+------------+--------------+--------------+
| name     | birth      | Franz' birth | age in years |
+----------+------------+--------------+--------------+
| Gretchen | 1942-04-14 | 1953-03-05   |           10 |
| Wilbur   | 1946-11-28 | 1953-03-05   |            6 |
+----------+------------+--------------+--------------+
2 rows in set (0.00 sec)mysql> select name, birth, curdate() as today, timestampdiff(month, birth, curdate()) as 'age in months' from sibling;
+----------+------------+------------+---------------+
| name     | birth      | today      | age in months |
+----------+------------+------------+---------------+
| Gretchen | 1942-04-14 | 2025-06-11 |           997 |
| Wilbur   | 1946-11-28 | 2025-06-11 |           942 |
| Franz    | 1953-03-05 | 2025-06-11 |           867 |
+----------+------------+------------+---------------+
3 rows in set (0.00 sec)mysql> select dayofyear('1995-03-01'), dayofyear('1996-02-29');
+-------------------------+-------------------------+
| dayofyear('1995-03-01') | dayofyear('1996-02-29') |
+-------------------------+-------------------------+
|                      60 |                      60 |
+-------------------------+-------------------------+
1 row in set (0.00 sec)mysql> select right('1995-03-01', 5), right('1996-02-29', 5);
+------------------------+------------------------+
| right('1995-03-01', 5) | right('1996-02-29', 5) |
+------------------------+------------------------+
| 03-01                  | 02-29                  |
+------------------------+------------------------+
1 row in set (0.00 sec)mysql> select if('02-29' < '03-01', '02-29', '03-01') as earliest;
+----------+
| earliest |
+----------+
| 02-29    |
+----------+
1 row in set (0.00 sec)mysql> set @birth = '1965-03-01';
Query OK, 0 rows affected (0.00 sec)mysql> set @target = '1975-01-01';
Query OK, 0 rows affected (0.00 sec)mysql> select @birth, @target, year(@target)- year(@birth) as 'difference',->         if(right(@target, 5) < right(@birth, 5), 1, 0) as 'adjustment',->         year(@target) - year(@birth) - if(right(@target, 5) < right(@birth, 5), 1, 0) as 'age';
+------------+------------+------------+------------+------+
| @birth     | @target    | difference | adjustment | age  |
+------------+------------+------------+------------+------+
| 1965-03-01 | 1975-01-01 |         10 |          1 |    9 |
+------------+------------+------------+------------+------+
1 row in set (0.00 sec)mysql> select name, birth, curdate() as today, year(curdate()) - year(birth) - if(right(curdate(), 5) < right(birth, 5), 1, 0) as 'age in years' from sibling;
+----------+------------+------------+--------------+
| name     | birth      | today      | age in years |
+----------+------------+------------+--------------+
| Gretchen | 1942-04-14 | 2025-06-11 |           83 |
| Wilbur   | 1946-11-28 | 2025-06-11 |           78 |
| Franz    | 1953-03-05 | 2025-06-11 |           72 |
+----------+------------+------------+--------------+
3 rows in set (0.00 sec)mysql> select name, birth, '1953-03-05' as 'Franz'' birthday', year('1953-03-05') - year(birth) - if(right('1953-03-05', 5) < right(birth, 5), 1, 0) as 'age in years' from sibling where name != 'Franz';
+----------+------------+-----------------+--------------+
| name     | birth      | Franz' birthday | age in years |
+----------+------------+-----------------+--------------+
| Gretchen | 1942-04-14 | 1953-03-05      |           10 |
| Wilbur   | 1946-11-28 | 1953-03-05      |            6 |
+----------+------------+-----------------+--------------+
2 rows in set (0.00 sec)mysql> select name, birth, curdate() as today, (year(curdate()) - year(birth)) * 12 + (month(curdate()) - month(birth)) - if(dayofmonth(curdate()) < dayofmonth(birth), 1, 0) as 'age in months' from sibling;
+----------+------------+------------+---------------+
| name     | birth      | today      | age in months |
+----------+------------+------------+---------------+
| Gretchen | 1942-04-14 | 2025-06-11 |           997 |
| Wilbur   | 1946-11-28 | 2025-06-11 |           942 |
| Franz    | 1953-03-05 | 2025-06-11 |           867 |
+----------+------------+------------+---------------+
3 rows in set (0.00 sec)--將一個日期和時間值切換到另一個時區
mysql> set @dt = '2006-11-23 09:00:00';
Query OK, 0 rows affected (0.00 sec)mysql> select @dt as Chicago, convert_tz(@dt, 'US/Central', 'Europe/Berlin') as Berlin,->         convert_tz(@dt, 'US/Central', 'Europe/London') as London,->         convert_tz(@dt, 'US/Central', 'America/Edmonton') as Edmonton,->         convert_tz(@dt, 'US/Central', 'Australia/Brisbane') as Brisbane\G
*************************** 1. row ***************************Chicago: 2006-11-23 09:00:00Berlin: NULLLondon: NULL
Edmonton: NULL
Brisbane: NULL
1 row in set (0.02 sec)mysql> select @dt as Chicago, convert_tz(@dt, '-06:00', '+01:00') as Berlin,->         convert_tz(@dt, '-06:00', '+00:00') as London,->         convert_tz(@dt, '-06:00', '-07:00') as Edmonton,->         convert_tz(@dt, '-06:00', '+10:00') as Brisbane\G
*************************** 1. row ***************************Chicago: 2006-11-23 09:00:00Berlin: 2006-11-23 16:00:00.000000London: 2006-11-23 15:00:00.000000
Edmonton: 2006-11-23 08:00:00.000000
Brisbane: 2006-11-24 01:00:00.000000
1 row in set (0.00 sec)

6.12 將一個日期和時間值切換到另一個時區

mysql> set @dt = '2006-11-23 09:00:00';
Query OK, 0 rows affected (0.00 sec)mysql> select @dt as Chicago, convert_tz(@dt, 'US/Central', 'Europe/Berlin') as Berlin,->         convert_tz(@dt, 'US/Central', 'Europe/London') as London,->         convert_tz(@dt, 'US/Central', 'America/Edmonton') as Edmonton,->         convert_tz(@dt, 'US/Central', 'Australia/Brisbane') as Brisbane\G
*************************** 1. row ***************************Chicago: 2006-11-23 09:00:00Berlin: NULLLondon: NULL
Edmonton: NULL
Brisbane: NULL
1 row in set (0.00 sec)mysql> select @dt as Chicago, convert_tz(@dt, '-06:00', '+01:00') as Berlin,->         convert_tz(@dt, '-06:00', '+00:00') as London,->         convert_tz(@dt, '-06:00', '-07:00') as Edmonton,->         convert_tz(@dt, '-06:00', '+10:00') as Brisbane\G
*************************** 1. row ***************************Chicago: 2006-11-23 09:00:00Berlin: 2006-11-23 16:00:00.000000London: 2006-11-23 15:00:00.000000
Edmonton: 2006-11-23 08:00:00.000000
Brisbane: 2006-11-24 01:00:00.000000
1 row in set (0.00 sec)

6.13 找出每月的第一天,最后一天或者天數

mysql> select d, date_format(d, '%Y-%m-01') as method1,->         concat(year(d), '-', lpad(month(d), 2, '0'), '-01') as method2-> from date_val;
+------------+------------+------------+
| d          | method1    | method2    |
+------------+------------+------------+
| 1864-02-28 | 1864-02-01 | 1864-02-01 |
| 1900-01-15 | 1900-01-01 | 1900-01-01 |
| 1999-12-31 | 1999-12-01 | 1999-12-01 |
| 2000-06-04 | 2000-06-01 | 2000-06-01 |
| 2017-03-16 | 2017-03-01 | 2017-03-01 |
| 1864-02-28 | 1864-02-01 | 1864-02-01 |
| 1900-01-15 | 1900-01-01 | 1900-01-01 |
| 1999-12-31 | 1999-12-01 | 1999-12-01 |
| 2000-06-04 | 2000-06-01 | 2000-06-01 |
| 2017-03-16 | 2017-03-01 | 2017-03-01 |
| 2007-05-13 | 2007-05-01 | 2007-05-01 |
+------------+------------+------------+
11 rows in set (0.01 sec)mysql>
mysql> select d, date_format(d, '%Y-01-01') as method1,->         concat(year(d), '-01-01') as method2-> from date_val;
+------------+------------+------------+
| d          | method1    | method2    |
+------------+------------+------------+
| 1864-02-28 | 1864-01-01 | 1864-01-01 |
| 1900-01-15 | 1900-01-01 | 1900-01-01 |
| 1999-12-31 | 1999-01-01 | 1999-01-01 |
| 2000-06-04 | 2000-01-01 | 2000-01-01 |
| 2017-03-16 | 2017-01-01 | 2017-01-01 |
| 1864-02-28 | 1864-01-01 | 1864-01-01 |
| 1900-01-15 | 1900-01-01 | 1900-01-01 |
| 1999-12-31 | 1999-01-01 | 1999-01-01 |
| 2000-06-04 | 2000-01-01 | 2000-01-01 |
| 2017-03-16 | 2017-01-01 | 2017-01-01 |
| 2007-05-13 | 2007-01-01 | 2007-01-01 |
+------------+------------+------------+
11 rows in set (0.00 sec)mysql>
mysql> select d, date_format(d, '%Y-12-15') as method1,->         concat(year(d), '-12-15') as method2-> from date_val;
+------------+------------+------------+
| d          | method1    | method2    |
+------------+------------+------------+
| 1864-02-28 | 1864-12-15 | 1864-12-15 |
| 1900-01-15 | 1900-12-15 | 1900-12-15 |
| 1999-12-31 | 1999-12-15 | 1999-12-15 |
| 2000-06-04 | 2000-12-15 | 2000-12-15 |
| 2017-03-16 | 2017-12-15 | 2017-12-15 |
| 1864-02-28 | 1864-12-15 | 1864-12-15 |
| 1900-01-15 | 1900-12-15 | 1900-12-15 |
| 1999-12-31 | 1999-12-15 | 1999-12-15 |
| 2000-06-04 | 2000-12-15 | 2000-12-15 |
| 2017-03-16 | 2017-12-15 | 2017-12-15 |
| 2007-05-13 | 2007-12-15 | 2007-12-15 |
+------------+------------+------------+
11 rows in set (0.00 sec)mysql>
mysql> select curdate(), date_add(date_format(curdate(), '%Y-12-25'), interval 2 year) as method1,->         date_format(date_add(curdate(), interval 2 year), '%Y-12-25') as method2;
+------------+------------+------------+
| curdate()  | method1    | method2    |
+------------+------------+------------+
| 2025-06-11 | 2027-12-25 | 2027-12-25 |
+------------+------------+------------+
1 row in set (0.00 sec)

6.14 通過子串替換來計算日期

mysql> select curdate(), dayname(curdate());
+------------+--------------------+
| curdate()  | dayname(curdate()) |
+------------+--------------------+
| 2025-06-11 | Wednesday          |
+------------+--------------------+
1 row in set (0.00 sec)
--一個月的第一天是星期幾
mysql> set @d = curdate();
Query OK, 0 rows affected (0.00 sec)mysql> set @first = date_sub(@d, interval dayofmonth(@d)-1 day);
Query OK, 0 rows affected (0.00 sec)mysql> select @d as 'starting date',->         @first as '1st of month date',->         dayname(@first) as '1st of month day';
+---------------+-------------------+------------------+
| starting date | 1st of month date | 1st of month day |
+---------------+-------------------+------------------+
| 2025-06-11    | 2025-06-01        | Sunday           |
+---------------+-------------------+------------------+
1 row in set (0.00 sec)

6.15 計算某個日期為星期幾

mysql> select d, dayname(d) as day,->         date_add(d, interval 1-dayofweek(d) day) as sunday,->         date_add(d, interval 7-dayofweek(d) day) as saturday-> from date_val;
+------------+----------+------------+------------+
| d          | day      | sunday     | saturday   |
+------------+----------+------------+------------+
| 1864-02-28 | Sunday   | 1864-02-28 | 1864-03-05 |
| 1900-01-15 | Monday   | 1900-01-14 | 1900-01-20 |
| 1999-12-31 | Friday   | 1999-12-26 | 2000-01-01 |
| 2000-06-04 | Sunday   | 2000-06-04 | 2000-06-10 |
| 2017-03-16 | Thursday | 2017-03-12 | 2017-03-18 |
| 1864-02-28 | Sunday   | 1864-02-28 | 1864-03-05 |
| 1900-01-15 | Monday   | 1900-01-14 | 1900-01-20 |
| 1999-12-31 | Friday   | 1999-12-26 | 2000-01-01 |
| 2000-06-04 | Sunday   | 2000-06-04 | 2000-06-10 |
| 2017-03-16 | Thursday | 2017-03-12 | 2017-03-18 |
| 2007-05-13 | Sunday   | 2007-05-13 | 2007-05-19 |
+------------+----------+------------+------------+
11 rows in set (0.00 sec)
--兩個星期前星期三的日期
mysql> set @target = date_sub(date_add(curdate(), interval 4-dayofweek(curdate()) day), interval 14 day);
Query OK, 0 rows affected (0.00 sec)mysql> select curdate(), @target, dayname(@target);
+------------+------------+------------------+
| curdate()  | @target    | dayname(@target) |
+------------+------------+------------------+
| 2025-06-11 | 2025-05-28 | Wednesday        |
+------------+------------+------------------+
1 row in set (0.00 sec)mysql>
mysql> set @target = date_add(date_sub(curdate(), interval 14 day), interval 4-dayofweek(curdate()) day);
Query OK, 0 rows affected (0.00 sec)mysql> select curdate(), @target, dayname(@target);
+------------+------------+------------------+
| curdate()  | @target    | dayname(@target) |
+------------+------------+------------------+
| 2025-06-11 | 2025-05-28 | Wednesday        |
+------------+------------+------------------+
1 row in set (0.00 sec)

6.16 查出給定某周的某天的日期

mysql> select d, dayname(d) as day,->         date_add(d, interval 1-dayofweek(d) day) as sunday,->         date_add(d, interval 7-dayofweek(d) day) as saturday-> from date_val;
+------------+----------+------------+------------+
| d          | day      | sunday     | saturday   |
+------------+----------+------------+------------+
| 1864-02-28 | Sunday   | 1864-02-28 | 1864-03-05 |
| 1900-01-15 | Monday   | 1900-01-14 | 1900-01-20 |
| 1999-12-31 | Friday   | 1999-12-26 | 2000-01-01 |
| 2000-06-04 | Sunday   | 2000-06-04 | 2000-06-10 |
| 2017-03-16 | Thursday | 2017-03-12 | 2017-03-18 |
| 1864-02-28 | Sunday   | 1864-02-28 | 1864-03-05 |
| 1900-01-15 | Monday   | 1900-01-14 | 1900-01-20 |
| 1999-12-31 | Friday   | 1999-12-26 | 2000-01-01 |
| 2000-06-04 | Sunday   | 2000-06-04 | 2000-06-10 |
| 2017-03-16 | Thursday | 2017-03-12 | 2017-03-18 |
| 2007-05-13 | Sunday   | 2007-05-13 | 2007-05-19 |
+------------+----------+------------+------------+
11 rows in set (0.00 sec)
--兩個星期前星期三的日期
set @target = date_sub(date_add(curdate(), interval 4-dayofweek(curdate()) day), interval 14 day);
select curdate(), @target, dayname(@target);set @target = date_add(date_sub(curdate(), interval 14 day), interval 4-dayofweek(curdate()) day);
select curdate(), @target, dayname(@target);

6.17 執行閏年計算

mysql> select d, year(d) % 4 = 0 as 'rule-of-thumb test',->         (year(d) % 4 = 0) and ((year(d) % 100 != 0) or (year(d) % 400 = 0))->         as 'complete test'-> from date_val;
+------------+--------------------+---------------+
| d          | rule-of-thumb test | complete test |
+------------+--------------------+---------------+
| 1864-02-28 |                  1 |             1 |
| 1900-01-15 |                  1 |             0 |
| 1999-12-31 |                  0 |             0 |
| 2000-06-04 |                  1 |             1 |
| 2017-03-16 |                  0 |             0 |
| 1864-02-28 |                  1 |             1 |
| 1900-01-15 |                  1 |             0 |
| 1999-12-31 |                  0 |             0 |
| 2000-06-04 |                  1 |             1 |
| 2017-03-16 |                  0 |             0 |
| 2007-05-13 |                  0 |             0 |
+------------+--------------------+---------------+
11 rows in set (0.00 sec)mysql>
mysql> set @d = '2006-04-13';
Query OK, 0 rows affected (0.00 sec)mysql> select dayofyear(date_format(@d, '%Y-12-31'));
+----------------------------------------+
| dayofyear(date_format(@d, '%Y-12-31')) |
+----------------------------------------+
|                                    365 |
+----------------------------------------+
1 row in set (0.00 sec)mysql>
mysql> set @d = '2008-04-13';
Query OK, 0 rows affected (0.00 sec)mysql> select dayofyear(date_format(@d, '%Y-12-31'));
+----------------------------------------+
| dayofyear(date_format(@d, '%Y-12-31')) |
+----------------------------------------+
|                                    366 |
+----------------------------------------+
1 row in set (0.00 sec)

6.18 接近但不是iso格式的日期格式

mysql> select d, concat(year(d), '-', month(d), '-01') from date_val;
+------------+---------------------------------------+
| d          | concat(year(d), '-', month(d), '-01') |
+------------+---------------------------------------+
| 1864-02-28 | 1864-2-01                             |
| 1900-01-15 | 1900-1-01                             |
| 1999-12-31 | 1999-12-01                            |
| 2000-06-04 | 2000-6-01                             |
| 2017-03-16 | 2017-3-01                             |
| 1864-02-28 | 1864-2-01                             |
| 1900-01-15 | 1900-1-01                             |
| 1999-12-31 | 1999-12-01                            |
| 2000-06-04 | 2000-6-01                             |
| 2017-03-16 | 2017-3-01                             |
| 2007-05-13 | 2007-5-01                             |
+------------+---------------------------------------+
11 rows in set (0.00 sec)mysql> select d, concat(year(d), '-', lpad(month(d), 2, '0'), '-01') from date_val;
+------------+-----------------------------------------------------+
| d          | concat(year(d), '-', lpad(month(d), 2, '0'), '-01') |
+------------+-----------------------------------------------------+
| 1864-02-28 | 1864-02-01                                          |
| 1900-01-15 | 1900-01-01                                          |
| 1999-12-31 | 1999-12-01                                          |
| 2000-06-04 | 2000-06-01                                          |
| 2017-03-16 | 2017-03-01                                          |
| 1864-02-28 | 1864-02-01                                          |
| 1900-01-15 | 1900-01-01                                          |
| 1999-12-31 | 1999-12-01                                          |
| 2000-06-04 | 2000-06-01                                          |
| 2017-03-16 | 2017-03-01                                          |
| 2007-05-13 | 2007-05-01                                          |
+------------+-----------------------------------------------------+
11 rows in set (0.00 sec)mysql>
mysql> select concat(year(d), '-', month(d), '-01') as 'non-iso',->         date_add(concat(year(d), '-', month(d), '-01'), interval 0 day) as 'iso 1',->         concat(year(d), '-', month(d), '-01') + interval 0 day as 'iso2',->         from_days(to_days(concat(year(d), '-', month(d), '-01'))) as 'iso 3',->         str_to_date(concat(year(d), '-', month(d), '-01'), '%Y-%m-%d') as 'iso 4'-> from date_val;
+------------+------------+------------+------------+------------+
| non-iso    | iso 1      | iso2       | iso 3      | iso 4      |
+------------+------------+------------+------------+------------+
| 1864-2-01  | 1864-02-01 | 1864-02-01 | 1864-02-01 | 1864-02-01 |
| 1900-1-01  | 1900-01-01 | 1900-01-01 | 1900-01-01 | 1900-01-01 |
| 1999-12-01 | 1999-12-01 | 1999-12-01 | 1999-12-01 | 1999-12-01 |
| 2000-6-01  | 2000-06-01 | 2000-06-01 | 2000-06-01 | 2000-06-01 |
| 2017-3-01  | 2017-03-01 | 2017-03-01 | 2017-03-01 | 2017-03-01 |
| 1864-2-01  | 1864-02-01 | 1864-02-01 | 1864-02-01 | 1864-02-01 |
| 1900-1-01  | 1900-01-01 | 1900-01-01 | 1900-01-01 | 1900-01-01 |
| 1999-12-01 | 1999-12-01 | 1999-12-01 | 1999-12-01 | 1999-12-01 |
| 2000-6-01  | 2000-06-01 | 2000-06-01 | 2000-06-01 | 2000-06-01 |
| 2017-3-01  | 2017-03-01 | 2017-03-01 | 2017-03-01 | 2017-03-01 |
| 2007-5-01  | 2007-05-01 | 2007-05-01 | 2007-05-01 | 2007-05-01 |
+------------+------------+------------+------------+------------+
11 rows in set (0.00 sec)

6.19 將日期或時間當成數值

mysql> select t1, t1+0 as 't1 as number',->     floor(t1) as 't1 as number',->     floor(t1/10000) as 'hour part'-> from time_val;
+----------+--------------+--------------+-----------+
| t1       | t1 as number | t1 as number | hour part |
+----------+--------------+--------------+-----------+
| 15:00:00 |       150000 |       150000 |        15 |
| 05:01:30 |        50130 |        50130 |         5 |
| 12:30:20 |       123020 |       123020 |        12 |
+----------+--------------+--------------+-----------+
3 rows in set (0.01 sec)mysql>
mysql> select d, d+0 from date_val;
+------------+----------+
| d          | d+0      |
+------------+----------+
| 1864-02-28 | 18640228 |
| 1900-01-15 | 19000115 |
| 1999-12-31 | 19991231 |
| 2000-06-04 | 20000604 |
| 2017-03-16 | 20170316 |
| 1864-02-28 | 18640228 |
| 1900-01-15 | 19000115 |
| 1999-12-31 | 19991231 |
| 2000-06-04 | 20000604 |
| 2017-03-16 | 20170316 |
| 2007-05-13 | 20070513 |
+------------+----------+
11 rows in set (0.00 sec)mysql> select dt, dt+0, floor(dt+0) from datetime_val;
+---------------------+----------------+----------------+
| dt                  | dt+0           | floor(dt+0)    |
+---------------------+----------------+----------------+
| 1970-01-01 00:00:00 | 19700101000000 | 19700101000000 |
| 1999-12-31 09:00:00 | 19991231090000 | 19991231090000 |
| 2000-06-04 15:45:30 | 20000604154530 | 20000604154530 |
| 2017-03-16 12:30:15 | 20170316123015 | 20170316123015 |
| 1970-01-01 00:00:00 | 19700101000000 | 19700101000000 |
| 1999-12-31 09:00:00 | 19991231090000 | 19991231090000 |
| 2000-06-04 15:45:30 | 20000604154530 | 20000604154530 |
| 2017-03-16 12:30:15 | 20170316123015 | 20170316123015 |
+---------------------+----------------+----------------+
8 rows in set (0.00 sec)mysql> select '1999-01-01' + 0, '1999-01-01 12:30:45' + 0, '12:30:45'+0;
+------------------+---------------------------+--------------+
| '1999-01-01' + 0 | '1999-01-01 12:30:45' + 0 | '12:30:45'+0 |
+------------------+---------------------------+--------------+
|             1999 |                      1999 |           12 |
+------------------+---------------------------+--------------+
1 row in set, 3 warnings (0.00 sec)mysql> show warnings;
+---------+------+---------------------------------------------------------+
| Level   | Code | Message                                                 |
+---------+------+---------------------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: '1999-01-01'          |
| Warning | 1292 | Truncated incorrect DOUBLE value: '1999-01-01 12:30:45' |
| Warning | 1292 | Truncated incorrect DOUBLE value: '12:30:45'            |
+---------+------+---------------------------------------------------------+
3 rows in set (0.00 sec)

6.20 強制mysql將字符串當作時間值

mysql> select '12:30:45'+0, sec_to_time(time_to_sec('12:30:45'))+0;
+--------------+----------------------------------------+
| '12:30:45'+0 | sec_to_time(time_to_sec('12:30:45'))+0 |
+--------------+----------------------------------------+
|           12 |                                 123045 |
+--------------+----------------------------------------+
1 row in set, 1 warning (0.00 sec)mysql> select '1999-01-01'+0, from_days(to_days('1999-01-01'))+0;
+----------------+------------------------------------+
| '1999-01-01'+0 | from_days(to_days('1999-01-01'))+0 |
+----------------+------------------------------------+
|           1999 |                           19990101 |
+----------------+------------------------------------+
1 row in set, 1 warning (0.00 sec)mysql> select date_add('1999-01-01 12:30:45', interval 0 day)+0 as 'numberic datetime';
+-------------------+
| numberic datetime |
+-------------------+
|    19990101123045 |
+-------------------+
1 row in set (0.00 sec)

6.21 基于時間特性來查詢行
https://blog.csdn.net/liqfyiyi/article/details/50886752

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/84507.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/84507.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/84507.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

SAP RESTFUL接口方式發布SICF實現全路徑

其他相關資料帖可參考&#xff1a; https://blog.csdn.net/woniu_maggie/article/details/146210752 https://blog.csdn.net/SAPmatinal/article/details/134349125 https://blog.csdn.net/weixin_44382089/article/details/128283417 【業務場景】 外部系統不想通過RFC (需…

在windows中安裝或卸載nginx

首先在nginx的安裝目錄下cmd查看nginx的版本&#xff1a; 在看windows的服務中是否nginx注冊為服務了 如果注冊了服務就先將服務卸載了 在nginx的安裝目錄cmd執行命令 NginxService.exe uninstall “NginxService”是對應的注冊的服務名稱 關閉所有的相關nginx的服務這個也…

FaceFusion 技術深度剖析:核心算法與實現機制揭秘

在 AI 換臉技術蓬勃發展的浪潮中&#xff0c;FaceFusion 憑借其出色的換臉效果和便捷的操作&#xff0c;成為眾多用戶的首選工具。從短視頻平臺上的創意惡搞視頻&#xff0c;到影視制作中的特效合成&#xff0c;FaceFusion 都展現出強大的實用性。而這一切的背后&#xff0c;是…

2. Web網絡基礎 - 協議端口

深入解析協議端口與netstat命令&#xff1a;網絡工程師的實戰指南 在網絡通信中&#xff0c;協議端口是服務訪問的門戶。本文將全面解析端口概念&#xff0c;并通過netstat命令實戰演示如何監控網絡連接狀態。 一、協議端口核心知識解析 1. 端口號的本質與分類 端口范圍類型說…

嵌入式學習筆記 - freeRTOS vTaskPlaceOnEventList()函數解析

vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait ); 函數第一個參數為消息隊列等待插入鏈表&#xff0c; void vTaskPlaceOnEventList( List_t * const pxEventList, const TickType_t xTicksToWait ) { configASSERT( pxEventList ); /…

Ubuntu 配置使用 zsh + 插件配置 + oh-my-zsh 美化過程

Ubuntu 配置使用 zsh 插件配置 oh-my-zsh 美化過程 引言zsh 安裝及基礎配置oh-my-zsh 安裝及美化配置oh-my-zsh 安裝主題美化配置主題自定義主題 插件安裝及配置官方插件查看及啟用插件安裝 主題文件備份.zshrcre5et_self.zsh-theme 同步發布在個人筆記Ubuntu 配置使用 zsh …

Xilinx FPGA 重構Multiboot ICAPE2和ICAPE3使用

一、FPGA Multiboot 本文主要介紹基于IPROG命令的FPGA多版本重構&#xff0c;用ICAP原語實現在線多版本切換。需要了解MultiBoot Fallback點擊鏈接。 如下圖所示&#xff0c;ICAP原語可實現flash中n1各版本的動態切換&#xff0c;在工作過程中&#xff0c;可以通過IPROG命令切…

springMVC-11 中文亂碼處理

前言 本文介紹了springMVC中文亂碼的解決方案&#xff0c;同時也貼出了本人遇到過的其他亂碼情況&#xff0c;可以根據自身情況選擇合適的解決方案。 其他-jdbc、前端、后端、jsp亂碼的解決 Tomcat導致的亂碼解決 自定義中文亂碼過濾器 老方法&#xff0c;通過javaW…

mysql-innoDB存儲引擎事務的原理

InnoDB 存儲引擎支持 ACID 事務&#xff0c;其事務機制是通過 Redo Log&#xff08;重做日志&#xff09;、Undo Log&#xff08;回滾日志&#xff09; 和 事務日志系統 來實現的。下面詳細解析 InnoDB 事務的工作原理。 1.事務的基本特性&#xff08;ACID&#xff09; 特性描…

在GIS 工作流中實現數據處理

通過將 ArcPy 應用于實際的 GIS 工作流&#xff0c;我們可以高效地完成數據處理任務&#xff0c;節省大量時間和精力。接下來&#xff0c;本文將結合具體案例&#xff0c;詳細介紹如何運用 ArcPy 實現 GIS 數據處理的全流程。 數據讀取與合并 假設我們有多個 shapefile 文件&a…

第十四屆藍橋杯_省賽B組(C).冶煉金屬

題目如下: 拿到題我們來看一下&#xff0c;題目的意思&#xff0c;就是求出N個記錄中的最大最小值&#xff0c;言外之意就是&#xff0c;如果超過了這個最大值不行&#xff0c;如果小于這個最小值也不行&#xff0c;所以我們得出&#xff0c;這道題是一個二分答案的題目&#x…

??Android 如何查看CPU架構?2025年主流架構有哪些??

在開發安卓應用或選購手機時&#xff0c;了解設備的CPU架構至關重要。不同的架構影響性能、兼容性和能效比。那么&#xff0c;??如何查看安卓設備的CPU架構&#xff1f;2025年主流架構有哪些&#xff1f;不同架構之間有什么區別&#xff1f;?? 本文將為你詳細解答。 ??1.…

飛算 JavaAI 2.0.0:開啟老項目迭代維護新時代

在軟件開發領域&#xff0c;老項目的迭代與維護一直是開發團隊面臨的難題。代碼邏輯混亂、技術棧陳舊、開發效率低下等問題&#xff0c;讓老項目改造猶如一場 “噩夢”。而飛算 JavaAI 2.0.0 版本的正式上線&#xff0c;通過三大核心能力升級&#xff0c;為老項目開發帶來了全新…

Linux初步介紹

Linux是一種開源的類Unix操作系統內核&#xff0c;廣泛應用于服務器、桌面、嵌入式設備等各種計算平臺。它由Linus Torvalds于1991年首次開發&#xff0c;因其穩定性、安全性和靈活性&#xff0c;被全球開發者和企業廣泛采用。 特點&#xff1a; 開放性&#xff08;開源&#…

OneNet + openssl + MQTT

1.OneNet 使用的教程 1.在網絡上搜索onenet&#xff0c;注冊并且登錄賬號。 2.產品服務-----物聯網服務平臺立即體驗 3.在底下找到立即體驗進去 4.產品開發------創建產品 5.關鍵是選擇MQTT&#xff0c;其他的內容自己填寫 6.這里產品以及開發完成&#xff0c;接下來就是添加設…

行為設計模式之Memento(備忘錄)

行為設計模式之Memento&#xff08;備忘錄&#xff09; 前言&#xff1a; 備忘錄設計模式&#xff0c;有點像vmware快照可以回滾&#xff0c;idea的提交記錄同樣可以混滾&#xff0c;流程引擎中流程可以撤銷到或者回滾到某個指定的狀態。 1&#xff09;意圖 在不破壞封裝性的…

動畫直播如何顛覆傳統?解析足球籃球賽事的數據可視化革命

在5G和AI技術快速發展的今天&#xff0c;體育賽事直播正在經歷一場深刻的變革。傳統視頻直播雖然能提供真實的比賽畫面&#xff0c;但在戰術可視化、數據深度和交互體驗方面存在明顯短板。而基于實時數據驅動的動畫直播技術&#xff0c;正通過創新的方式彌補這些不足&#xff0…

二刷蒼穹外賣 day01

nginx nginx反向代理 將前端發送的請求由nginx轉發到后端服務器 好處&#xff1a; 提速&#xff1a;nginx本身可緩存數據 負載均衡&#xff1a;配置多臺服務器&#xff0c;大量請求來臨可均衡分配 保證后端安全&#xff1a;不暴露后端服務真實地址 server{listen 80;server_…

5.2 HarmonyOS NEXT應用性能診斷與優化:工具鏈、啟動速度與功耗管理實戰

HarmonyOS NEXT應用性能診斷與優化&#xff1a;工具鏈、啟動速度與功耗管理實戰 在HarmonyOS NEXT的全場景生態中&#xff0c;應用性能直接影響用戶體驗。通過專業的性能分析工具鏈、針對性的啟動速度優化&#xff0c;以及精細化的功耗管理&#xff0c;開發者能夠構建"秒…

模型訓練-關于token【低概率token, 高熵token】

Qwen團隊新發現&#xff1a;大模型推理能力的提高僅由少數高熵 Token 貢獻 不要讓低概率token主導了LLM的強化學習過程 一 低概率詞元問題 論文&#xff1a;Do Not Let Low-Probability Tokens Over-Dominate in RL for LLMs 在RL訓練過程中&#xff0c;低概率詞元&#xff08…