Loading

mysql,一些简单的时间处理

1.将时间增加一天

SELECT DATE_ADD(create_at,INTERVAL 1 DAY) FROM courses;

2.计算从 2019 年 03 月 26 日到创建时间(created_at)相差的天数,结果列名以 date_diff 显示

select DATEDIFF(created_at,"2019-03-26") AS date_diff From courses

3.从课程表 courses 中查询所有课程的课程名称( name )和课程创建时间( created_at )的小时数,将提取小时数的列名起别名为 created_hour

SELECT name,HOUR(created_at) AS created_hour FROM courses

4.日期均提前一天

DATE_SUB(`created_at`,interval 1 day)

5.查询 courses 表,计算 '2020-04-22' 与课程创建时间的月数差,返回列名显示为 MonthDiff

SELECT timestampdiff(month,created_at,'2020-04-22') as MonthDiff from courses 

6.课程表 courses 中查询 2020 年 8 月前的课程名和创建日期,并为创建日期的列名起别名为 created_date (日期指 created_at 中不包括具体时间的部分)

SELECT name,date(created_at)as created_date from courses WHERE created_at <"2020-08-01"

7.查询 courses 表,查询课程创建时间,按照 ’yyyy-MM-dd HH:mm:ss’ 的格式返回结果,返回列名显示为 DATE_FORMAT

SELECT date_format(created_at,"%Y-%m-%d %H:%i:%s") as DATE_FORMAT from courses;

8.向记录表 records 中插入当前的日期

INSERT into  records(now_time) VALUES (now())

附表

mysql> select * from courses;
+------+-------------------------+---------------+-------------------+------------+
| id   | name                    | student_count | create_at         | teacher_id |
+------+-------------------------+---------------+-------------------+------------+
|    2 | System Design           | 1350          | 2020-7-18         |          3 |
|    3 | Django                  | 780           | 2020-2-29         |          3 |
|    4 | Web                     | 340           | 2020-4-22         |          4 |
|    5 | Big Data                | 700           | 2020-9-11         |          1 |
|    6 | Artificial Intelligence | 1660          | 2018-5-13         |          3 |
|    7 | Java P6+                | 780           | 2019-1-19         |          3 |
|    8 | Data Analysis           | 500           | 2019-7-12         |          1 |
|   10 | Object Oriented Design  | 300           | 2020-8-8          |          4 |
|   12 | Dynamic Programming     | 2000          | 2018-8-18         |          1 |
|    1 | Senior Algorithm        | 880           | 2020-6-1 09:03:12 |          4 |
+------+-------------------------+---------------+-------------------+------------+
10 rows in set (0.00 sec)
posted @ 2022-11-29 09:27  夷某蓁  阅读(73)  评论(0)    收藏  举报