SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Select statements using dates?

    I have a table that contains a table with the following column

    date datetime 0000-00-00 00:00:00
    I need to run the following queries, however, I'm unsure about what to point in the where portion of the statement.

    SELECT COUNT(*) FROM table where date = today's date;
    SELECT COUNT(*) FROM table where date = this month
    SELECT COUNT(*) FROM table where date = last month


    I'm unsure how to connect today's date, month, and last month to the format 0000-00-00 00:00:00 to get a count.

    Any suggestions?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,340
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    today
    Code:
    WHERE `date` >= CURRENT_DATE
      AND `date`  < CURRENT_DATE + INTERVAL 1 DAY
    this month
    Code:
    WHERE `date` >= CURRENT_DATE - INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY
      AND `date`  < CURRENT_DATE - INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY
                                 + INTERVAL 1 MONTH
    last month
    Code:
    WHERE `date` >= CURRENT_DATE - INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY
                                 - INTERVAL 1 MONTH
      AND `date`  < CURRENT_DATE - INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rudy you are a saint! thanks so much!

  4. #4
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    These queries worked perfectly. Thanks again!

    Just a quick question

    Lets say that I added another column name: record_id

    In this column I could have records such as

    abc
    xyz
    def
    abc

    Sometimes the records will be duplicate.

    However, I want the count to ignore the duplicate records of that column.

    So I'm thinking its something like this but unsure of the syntax:

    Code:
    WHERE `date` >= CURRENT_DATE - INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY
                                 - INTERVAL 1 MONTH
      AND `date`  < CURRENT_DATE - INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY
      AND NOT 'record_id' = duplicate?

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,340
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    SELECT COUNT(DISTINCT record_id) FROM ...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •