SQL在日期之间多年

发布于 2025-02-09 07:00:52 字数 301 浏览 1 评论 0原文

我正在Oracle Devgym上免费课程,我遇到了一个关于我不熟悉的日期的问题。如果我有一张带日期的销售表,并且我想在销售表中每年的每两个月中只显示任何两个月之间的日期。这就是我拥有的,但我不知道该怎么做多年,没有在这些年之间显示所有内容。例如,我正在尝试在餐桌上每年的八月至12月之间显示所有日期的销售。是否可以在不知道桌子上所有年份的情况下做到这一点?

Select *
From Sale
Where SaleDate BETWEEN '08/01/2013' and '12/31/2013'
Order by SaleDate

I'm doing free courses in the Oracle devgym and I came across a question about dates that I am unfamiliar with. If I have a table of sales with dates and I want to display only dates between any two given months for every possible year within the sales table how would I go about doing that. This is what I have but I don't know how to do it for multiple years without is showing everything in between those years also. I'm trying for instance to show all sales for dates in between August and December for every year in the table. Is it possible to do without knowing all the years within the table up front?

Select *
From Sale
Where SaleDate BETWEEN '08/01/2013' and '12/31/2013'
Order by SaleDate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南七夏 2025-02-16 07:00:52

您可以使用提取(从日期开始)获取日期的月份

Select *
From Sale
Where EXTRACT(MONTH FROM SaleDate) BETWEEN 8 and 12
Order by SaleDate

You can use EXTRACT(MONTH FROM date) to get the month of a date

Select *
From Sale
Where EXTRACT(MONTH FROM SaleDate) BETWEEN 8 and 12
Order by SaleDate
任性一次 2025-02-16 07:00:52

解决此问题的最佳方法是在MySQL中使用Dayofyear()函数,这使事情变得非常容易。它的价值从一年中的当天返回1至366。这完全消除了这一年的限制,还维持了一年的界限。如果您需要在几年之间搜索,则必须指定年度数字。
从销售中选择 *在销售中销售的dayofyear(2013/08/01/2013)和dayofyear(12/31/2013)订单订购;>

The best way to tackle this is to use the DAYOFYEAR() function in MYSQL which makes things very easy. It returns a value from 1 to 366 for the day of the year. This totally eliminates the year restriction ALSO maintaining the bound of a single year. If you need to search between years, you have to specify the year numbers.
Select * From Sale Where SaleDate BETWEEN DAYOFYEAR(08/01/2013) and DAYOFYEAR(12/31/2013) Order by SaleDate;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文