板凳-------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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈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…