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?
Thanks.
You can use DATE() from MySQL to select records with a particular date. The syntax is as follows. SELECT *from yourTableName WHERE DATE(yourDateColumnName)='anyDate'; To understand the above syntax, let us first create a table.
The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified condition.
I think the function you're looking for is MONTH(date)
. You'll probably want to use 'YEAR' too.
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
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
Which would return:
id --- 1 3 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With