问题描述
我希望能够为存储过程提供一个月和一年的时间,并让它返回该月发生的所有事情,我该怎么做,因为我无法进行比较,因为有些月份有不同的天数等等?
I want to be able to give a stored procedure a Month and Year and have it return everything that happens in that month, how do I do this as I can't compare between as some months have different numbers of days etc?
这样做的最佳方法是什么?我可以要求按年和月比较吗?
What's the best way to do this? Can I just ask to compare based on the year and month?
谢谢.
推荐答案
我认为您正在寻找的功能是 MONTH(date)
.您可能还想使用 'YEAR'.
I think the function you're looking for is MONTH(date)
. You'll probably want to use 'YEAR' too.
假设您有一个名为 things
的表,如下所示:
Let's assume you have a table named things
that looks something like this:
id happend_at
-- ----------------
1 2009-01-01 12:08
2 2009-02-01 12:00
3 2009-01-12 09:40
4 2009-01-29 17:55
假设您要执行以查找在 2009/01 月(2009 年 1 月)具有 happened_at
的所有记录.SQL 查询将是:
And let's say you want to execute to find all the records that have a happened_at
during the month 2009/01 (January 2009). The SQL query would be:
SELECT id FROM things
WHERE MONTH(happened_at) = 1 AND YEAR(happened_at) = 2009
哪个会返回:
id
---
1
3
4
这篇关于WHERE 子句查找特定月份的所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!