本文共 3311 字,大约阅读时间需要 11 分钟。
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 date from the system can be very handy for processing data using SQL.
从系统获取日期对于使用SQL处理数据非常方便。
-- current dateselect now(), sysdate(), current_date(), current_time(), -- date and time from the system on executiondayofyear(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查询中,我们看到以下内容:
You can also use GETDATE() to get the current date.
您也可以使用GETDATE()获取当前日期。
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:
在这里我们看到:
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 exampleselect * 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.
与所有这些内容一样,它们比本入门指南中的内容要多得多。 我希望这至少能给您足够的入门。 请参阅数据库管理员的手册,并尝试自己尝试其他选项,这很有趣。
翻译自:
getdate函数
转载地址:http://mduzd.baihongyu.com/