Mysql 简明教程

MySQL - Today Date

通常,日期使用三个值表示:日期、月份和年份。日期有许多可能的变体,所有这些都取决于几个不一致因素。

Generally, the date is represented using three values: date, month, and year. Dates have many possible variations, all of which depend on several inconsistency factors.

  1. DD/MM/YYYY, For instance - 04/04/2024

  2. YYYY/MM/DD, For instance - 2024/04/27

  3. DD-MM-YYYY, For instance - 04-04-2024

MySQL Today’s Date

我们有几个内置函数来检索和操作 MySQL 的今天的日期。以下是该功能:CURDATE()、CURRENT_DATE()、CURRENT_DATE。

We have several built-in functions to retrieve and manipulate the MySQL today’s date. The following are the functions: CURDATE(), CURRENT_DATE(), CURRENT_DATE.

CURDATE():此函数将当前日期作为“YYYY-MM-DD”(字符串)或“YYYYMMDD”(数字)返回。

CURDATE(): This function returns the current date as ‘YYYY-MM-DD’ (string) or ‘YYYYMMDD’ (numeric).

CURRENT_DATE():此函数是 CURDATE() 函数的同义词,它以相同的格式返回当前日期。

CURRENT_DATE(): This function is s synonym to the CURDATE() function which returns the current date in the same format.

CURRENT_DATE:这也是 CURDATE() 函数的同义词。

CURRENT_DATE: This is also synonym of CURDATE() function.

MySQL CURDATE() Function

在以下示例中,我们使用 CURDATE() 函数检索当前日期值:

In the following example, we are retrieving the current date value using the CURDATE() function −

**

SELECT CURDATE() AS Today;

Output

执行给定的查询后,输出如下:

On executing the given query, the output is displayed as follows −

MySQL CURRENT_DATE() Function

同样,我们还可以使用 CURRENT_DATE() 函数显示当前日期值。

Similarly, we can also display the current date value using the CURRENT_DATE() function.

SELECT CURRENT_DATE() AS Today;

Output

执行给定的查询后,输出如下:

On executing the given query, the output is displayed as follows −

MySQL CURRENT_DATE Function

在此示例中,我们使用 CURRENT_DATE 函数来检索系统所在位置的当前日期。

In this example, we use the CURRENT_DATE function to retrieve the current date local to a system.

SELECT CURRENT_DATE AS Today;

Output

执行给定的查询后,输出如下:

On executing the given query, the output is displayed as follows −

Inserting Date Values in a Table

以下是将日期和时间值插入表中的步骤:

Following are the steps to insert date and time values in a table −

  1. First, we must create a table that accepts date and time values.

  2. Second, we must insert the data into the newly created table, which accepts date and time data types.

Example

现在,让我们使用以下查询创建一个名为 ORDERS 的表 -

Now, let us a create a table with the name ORDERS using the following query −

CREATE TABLE ORDERS (
   OID INT NOT NULL,
   DATE VARCHAR (20) NOT NULL,
   CUSTOMER_ID INT NOT NULL,
   AMOUNT DECIMAL (18, 2)
);

在这里,我们使用INSERT INTO语句将值插入到上述创建的表中,如下所示 -

Here, we are inserting values into the above-created table using the INSERT INTO statement as shown below −

INSERT INTO ORDERS VALUES
(102, CURDATE() + 1, 3, 3000.00),
(100, CURDATE() - 5, 3, 1500.00),
(101, CURRENT_DATE() - 2, 2, 1560.00),
(103, CURRENT_DATE + 3, 4, 2060.00);

该表创建如下 −

The table is created as follows −

Today Date Using Client Program

我们还可以使用客户端程序执行今日日期。

We can also perform Today Date Using Client Program.

Syntax

Example

以下是这些程序 −

Following are the programs −