MySQL中日期怎么表示
在MySQL中,日期和时间可以使用多种数据类型来表示,本文将介绍这些数据类型及其用法,并提供一些示例代码。
1. DATE
DATE数据类型用于存储不带时间信息的日期,它只包含年、月和日三个字段,格式为'YYYY-MM-DD'。
CREATE TABLE orders ( id INT PRIMARY KEY AUTO_INCREMENT, order_date DATE );
插入一条记录:
INSERT INTO orders (order_date) VALUES ('2023-07-03');
查询所有订单的日期:
SELECT order_date FROM orders;
结果:
+------------+--------------+ | order_date | 2023-07-03 | +------------+--------------+ | order_date | 2023-07-04 | | order_date | 2023-07-05 | | order_date | 2023-07-06 | +------------+--------------+
2. DATETIME
DATETIME数据类型用于存储带有日期和时间信息的数据,它包含四个字段,分别表示年、月、日和时、分、秒,格式为'YYYY-MM-DD HH:MI:SS'。
CREATE TABLE events ( id INT PRIMARY KEY AUTO_INCREMENT, event_name VARCHAR(255), event_time DATETIME );
插入一条记录:
INSERT INTO events (event_name, event_time) VALUES ('Meeting', '2023-07-03 14:30:00');
查询所有事件的时间:
SELECT event_time FROM events;
结果:
+---------------------+-------------------------+----------+--------------------+------------------+------------------+--------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+-------------------------+---------------------+-------------------------+----------+--------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------+-------------------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+-------------------------+-------------------------+------------+--------------------+--------------------+-------------------------+-------------------------+-------------------------+-------------------------+-------------------------+-------------------------+-------------------------++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/263236.html