SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Question about using DATETIME type

    I want to select the members count from my members table that signed up on the current date, not date and time. My field is a datetime field however. Would this still work?

    SELECT COUNT(*) FROM members WHERE created = CURDATE() ? If not how would I do it? Could I do

    SELECT COUNT(*) FROM members WHERE created LIKE '%' . CURDATE() ? Thanks for the help.
    Have a good day.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,262
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    you should never use LIKE with temporal datatypes (DATE, TIME, DATETIME)

    the following will work correctly --
    Code:
    WHERE DATE(created) = CURRENT_DATE
    however, that won't scale, because it requires a table scan -- pplying a function to a column means that the database cannot utilize an index on the column

    best practice is to use a range test with an open upper end --
    Code:
    WHERE created >= CURRENT_DATE
      AND created  < CURRENT_DATE + INTERVAL 1 DAY
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah thank you Rudy, btw I bought your book... should have probably went to look there first lol.
    Have a good day.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •