Sahriar rahman supto
Sahriar rahman supto

Reputation: 778

compare current month and year with timestamp

How to compare month and year only from timestamp? my timestamp formate in mysql is like this

2015-02-15 15:00:09

What will be the sql to get row on specific month and year? I use DATE to compare current date with timestamp.

$q="SELECT * FROM products WHERE DATE(time) = CURDATE()";

Now how to compare current month year with my timestamp? I searched here but didnt find any clear answer. Please help me.

Upvotes: 0

Views: 1244

Answers (2)

Madhivanan
Madhivanan

Reputation: 13700

If the column Time is indexed, then this is the efficient method

$q="SELECT * FROM products 
WHERE time >date_add(current_date,interval - day(current_date) day) and
Time<date_add(date_add(current_date,interval - day(current_date)+1 day),interval 1 month)"

Upvotes: 0

gurkov
gurkov

Reputation: 189

$q="SELECT * FROM products WHERE MONTH(time) = MONTH(CURDATE()) and YEAR(time) = YEAR(CURDATE)";

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

Upvotes: 2

Related Questions