SQL在日期之间多年
我正在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
提取(从日期开始)
获取日期的月份You can use
EXTRACT(MONTH FROM date)
to get the month of a date解决此问题的最佳方法是在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;