博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
getdate函数_SQL日期函数和GETDATE解释为带有语法示例
阅读量:2525 次
发布时间:2019-05-11

本文共 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 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 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查询中,我们看到以下内容:

  • 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 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/

你可能感兴趣的文章
Linux IPC实践(3) --具名FIFO
查看>>
Qt之模拟时钟
查看>>
第一次接触安卓--记于2015.8.21
查看>>
(转)在分层架构下寻找java web漏洞
查看>>
mac下多线程实现处理
查看>>
C++ ifstream ofstream
查看>>
跟初学者学习IbatisNet第四篇
查看>>
seL4环境配置
查看>>
Git报错:insufficient permission for adding an object to repository database .git/objects
查看>>
ajax跨域,携带cookie
查看>>
BZOJ 1600: [Usaco2008 Oct]建造栅栏( dp )
查看>>
洛谷 CF937A Olympiad
查看>>
Codeforces Round #445 C. Petya and Catacombs【思维/题意】
查看>>
用MATLAB同时作多幅图
查看>>
python中map的排序以及取出map中取最大最小值
查看>>
ROR 第一章 从零到部署--第一个程序
查看>>
<form>标签
查看>>
vue去掉地址栏# 方法
查看>>
Lambda03 方法引用、类型判断、变量引用
查看>>
was集群下基于接口分布式架构和开发经验谈
查看>>