getdate函数_SQL日期函数和GETDATE解释为带有语法示例

getdate函数

There are 61 Date Functions defined in MySQL. Don’t worry, we won’t review them all here. This guide will give you an introduction to some of the common ones, and enough exposure for you to comfortably to explore on your own.

MySQL中定义了61种日期函数。 不用担心,我们不会在这里全部审查。 本指南将向您介绍一些常见的知识,并为您提供足够的机会让您轻松地自行探索。

We will cover:

我们将介绍:

  • Getting the current date

    获取当前日期
  • Date Math

    日期数学
  • Dates in a where or having clause

    在where或having子句中的日期

获取当前日期 (Getting the current date)

Getting the date from the system can be very handy for processing data using SQL.

从系统获取日期对于使用SQL处理数据非常方便。

-- current date
select now(), sysdate(), current_date(), current_time(), -- date and time from the system on execution
dayofyear(now()) as NumDaysSoFarThisYr,
EXTRACT(YEAR FROM now()) as theYearPart,
EXTRACT(YEAR_MONTH FROM now()) as theYrMonPart, 
date_format(now(), '%W %M %Y') as oneOfManyFormats; 
;

In SQL query, we see the following:

在SQL查询中,我们看到以下内容:

  • The first two columns in the result are two ways to get the same information: the date on the system at the time the SQL is executed.

    结果的前两列是获得相同信息的两种方法:执行SQL时系统上的日期。
  • The next two columns slice out just the Date and Time parts of the system date.

    接下来的两列仅切出系统日期的日期和时间部分。
  • The next one presents the “day number” of the system date in this year. You’ll notice that this is one day more than the math shown in the next example.

    下一个表示今年系统日期的“天数”。 您会注意到,这比下一个示例中显示的数学要多一天。
  • The next two extract just the year and then both the year and month

    接下来的两个仅提取年份,然后提取年份和月份
  • Last, but not least, there is a single example of one of the many ways to format this dates.

    最后但并非最不重要的是,存在一个格式化此日期的多种方法之一的单个示例。

You can also use GETDATE() to get the current date.

您也可以使用GETDATE()获取当前日期。

日期数学 (Date Math)

select now(), current_date(), 
datediff(now(),'2017-01-01') as daysThisYear, 
subdate(current_date(), interval 150 day) as '150DaysAgo', 
adddate(now(), interval 7 day) as dateInA_Week -- date in a week
;

Here we see:

在这里我们看到:

  • The first two columns are just the system date and time for reference.

    前两列只是系统日期和时间供参考。
  • The second column is the date difference (datediff) between the first of January 2017 and the system date.

    第二列是2017年1月1日与系统日期之间的日期差(datediff)。
  • The last two columns are examples of subtracting and adding dates.

    最后两列是减去和添加日期的示例。

在where或having子句中 (In a where or having clause)

Here are two examples of using date math in a where clause:

这是在where子句中使用日期数学的两个示例:

select * from student; - to show the current data being used for the example
select * from student where recordCreated < '2017-01-01';
select * from student where recordCreated < subdate(current_date(), interval 225 day);

Regarding the HAVING part: Keep in mind, most of the WHERE clause logic will also work in the HAVING clause of a GROUP BY. The difference between the two is that the WHERE clause runs against the full data, and the HAVING runs against the data aggregated by the GROUP BY clause.

关于HAVING部分:请记住,大多数WHERE子句逻辑也可以在GROUP BY的HAVING子句中使用。 两者之间的区别在于WHERE子句针对完整数据运行,而HAVING子句针对GROUP BY子句聚合的数据运行。

As with all of these things there is MUCH MORE to them than what’s in this introductory guide. I hope this at least gives you enough to get started. Please see the manual for your database manager and have fun trying different options yourself.

与所有这些内容一样,它们比本入门指南中的内容要多得多。 我希望这至少能给您足够的入门。 请参阅数据库管理员的手册,并尝试自己尝试其他选项,这很有趣。

翻译自: https://www.freecodecamp.org/news/sql-date-functions-getdate/

getdate函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值