Page 1 of 1

MySQL - Select data in current day, week, month or year

Posted: 29 Dec 2014, 09:24
by Marius
I facing a problem about a select query in MySQL.
I have table user, It contains id_user, username and user_time. And user_time is a Date-Time for user when register, the properties is Timestamp so the output is like this:

Code: Select all

User A | user_time > 2014-12-22 10:10:10
I use this query for display data per year.

Code: Select all

SELECT * FROM user WHERE user_time = YEAR(NOW());
The query works but just giving an empty row. It's not only year but also give empty rows on day, week, or month.
Any answer?

MySQL - Select data in current day, week, month or year

Posted: 29 Dec 2014, 09:30
by Admin
Hi,
YEAR returns a number like 2015. That will never be equal to a time. You need to test whether the year of "user_time" is the same as the given year:

Code: Select all

SELECT * FROM user WHERE YEAR(user_time) = YEAR(NOW())