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

Discuss coding issues, and scripts related to PHP and MySQL.
Marius
Posts:107

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

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?

Admin Posts:805
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())

Similar Topics