138

I'm trying to run a mysql select statement where it looks at today's date and only returns results that signed up on that current day. I've currently tried the following, but it doesn't seem to work.

SELECT users.id, DATE_FORMAT(users.signup_date, '%Y-%m-%d') 
    FROM users 
    WHERE users.signup_date = CURDATE()

I've modified my SELECT statement to this, thanks guys.

SELECT id FROM users WHERE DATE(signup_date) = CURDATE()

4
  • 1
    it seems that signup_date is datetime field Oct 1, 2012 at 17:02
  • 1
    @Serjio Yes, it currently is a datetime field.
    – Jako
    Oct 1, 2012 at 17:03
  • what is the datatype of signup_date if it contains the time, then you will want to use the date_format in the WHERE clause to strip the time to match the CURDATE()
    – Taryn
    Oct 1, 2012 at 17:04
  • @bluefeet thanks, I've modified my script. It seems to work, but when there are no results. mysql_num_rows doesn't display 0. Just a blank.
    – Jako
    Oct 1, 2012 at 17:06

5 Answers 5

258
SELECT users.id, DATE_FORMAT(users.signup_date, '%Y-%m-%d') 
FROM users 
WHERE DATE(signup_date) = CURDATE()
6
  • is it necessary to use DATE() even the field is type Date ?
    – rashidnk
    Aug 7, 2015 at 10:34
  • 2
    @rashidnk No. Although he didn't say clearly in the question, the field is apparently of type DATETIME.
    – Barmar
    Aug 7, 2015 at 13:28
  • 3
    While this solution is absolutely correct, it does not scale well (cannot use an index on signup_date, even if such an index exists). Prefer Serjio's solution.
    – RandomSeed
    Oct 21, 2015 at 8:01
  • or if to find on a particular date, with the index also being utilised ``` SELECT users.id, DATE_FORMAT(users.signup_date, '%Y-%m-%d') FROM users where signup_date >= '2017-08-30 00:00:00' and signup_date <= '2017-08-30 23:59:59' ; ``` Oct 26, 2017 at 12:17
  • 3
    @SumitMAsok Better to use < '2017-08-31 00:00:00' in case you're using a version of MySQL with milliseconds.
    – Barmar
    Oct 26, 2017 at 15:56
44

This query will use index if you have it for signup_date field

SELECT users.id, DATE_FORMAT(users.signup_date, '%Y-%m-%d') 
    FROM users 
    WHERE signup_date >= CURDATE() && signup_date < (CURDATE() + INTERVAL 1 DAY)
4
  • 2
    Downvotes to this answer are absurd. This is in fact the prefered approach, because this query would be able to leverage an index on signup_date, unlike other approaches.
    – RandomSeed
    Oct 21, 2015 at 8:03
  • 2
    @RandomSeed The general idea of testing for signup_date between two times is correct, but the times are not. It should be between 00:00 and 23:59:59 on the current date.
    – Barmar
    Oct 21, 2015 at 9:16
  • Well the date interval was wrong too >.< The downvotes were not that absurd after all.
    – RandomSeed
    Oct 21, 2015 at 10:17
  • This query is way more performant (due to the index). The WHERE DATE(signup_date) = CURDATE() took my server ~50 ms, this one here 0.8 ms.
    – Avatar
    Apr 7, 2020 at 7:02
19

Sounds like you need to add the formatting to the WHERE:

SELECT users.id, DATE_FORMAT(users.signup_date, '%Y-%m-%d') 
FROM users 
WHERE DATE_FORMAT(users.signup_date, '%Y-%m-%d') = CURDATE()

See SQL Fiddle with Demo

0
3

You can use the CONCAT with CURDATE() to the entire time of the day and then filter by using the BETWEEN in WHERE condition:

SELECT users.id, DATE_FORMAT(users.signup_date, '%Y-%m-%d') 
FROM users 
WHERE (users.signup_date BETWEEN CONCAT(CURDATE(), ' 00:00:00') AND CONCAT(CURDATE(), ' 23:59:59'))
1
  • by far the most performant method.. not even comparable when running on large data sets. Multiple orders of magnitude faster. Thanks
    – dwkd
    Nov 3, 2022 at 22:25
1

This is a simple code to find date

SELECT users.id, DATE_FORMAT(users.signup_date, '%Y-%m-%d') 
FROM users 
WHERE DATE_FORMAT(users.signup_date, '%Y-%m-%d') = CURDATE()
1
  • I've edited out the link to your answer that you posted on another site, as it's not relevant to this post. In the future, when linking to external content, make sure it's relevant, and especially make sure to disclose any affiliation. If you don't do so you can be considered a spammer. Please see this for more details.
    – cigien
    Jul 31, 2021 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.