题解 | #插入记录(一)#
插入记录(一)
https://www.nowcoder.com/practice/5d2a42bfaa134479afb9fffd9eee970c
insert into exam_record(uid, exam_id, start_time, submit_time, score) values (1001, 9001, '2021-09-01 22:11:12', '2021-09-01 22:11:12' + interval 50 minute, 90), (1002, 9002, '2021-09-04 07:01:02', null, null) #一、插入记录的方式汇总: #普通插入(全字段):INSERT INTO table_name VALUES (value1, value2, ...) #普通插入(限定字段):INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...) #多条一次性插入:INSERT INTO table_name (column1, column2, ...) VALUES (value1_1, value1_2, ...), (value2_1, value2_2, ...), ... #从另一个表导入:INSERT INTO table_name SELECT * FROM table_name2 [WHERE key=value] #二、interval用法 # 1. 使用`DATE_ADD`函数: # 1.1 基本用法:SELECT DATE_ADD(NOW(), INTERVAL 1 DAY) AS tomorrow; # 1.2 指定时间单位:SELECT DATE_ADD(NOW(), INTERVAL 2 HOUR) AS two_hours_later; # 2. 使用`DATE_SUB`函数: # 2.1 基本用法:SELECT DATE_SUB(NOW(), INTERVAL 1 WEEK) AS one_week_ago; # 2.2 指定时间单位:SELECT DATE_SUB(NOW(), INTERVAL 3 MONTH) AS three_months_ago; # 3. 直接使用`INTERVAL`:SELECT NOW() + INTERVAL 5 DAY AS five_days_later; # 4. 示例:计算两个日期之间的差距:SELECT DATEDIFF('2024-02-05', '2024-01-01') AS date_difference; # 5. 示例:计算时间差:SELECT TIMEDIFF('14:30:00', '12:45:00') AS time_difference;