Get data during last 24 hours

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    Get data during last 24 hours

    I want to get data which are enter to the database during last 24 hours. This is my query and it is not working. Could anyone please tell me what is wrong in this?

    [PHP]SELECT FROM rainfall WHERE Date < DATE_SUB( NOW(), INTERVAL 24 HOUR)[/PHP]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Originally posted by ghjk
    I want to get data which are enter to the database during last 24 hours. This is my query and it is not working. Could anyone please tell me what is wrong in this?

    [PHP]SELECT FROM rainfall WHERE Date < DATE_SUB( NOW(), INTERVAL 24 HOUR)[/PHP]
    You must select something, which you do not in your query. It is also easier to use the TIMEFIDD() function where you can specify the wanted time difference, as follows [code=sql]SELECT * from rainfall where TIMEDIFF(date_t ime, now()) < '24:00:00';[/code]Ronald
    Last edited by ronverdonk; Apr 5 '08, 09:37 AM. Reason: typo

    Comment

    • ghjk
      Contributor
      • Jan 2008
      • 250

      #3
      Originally posted by ronverdonk
      You must select something, which you do not in your query. It is also easier to use the TIMEFIDD() function where you can specify the wanted time difference, as follows [code=sql]SELECT * from rainfall where TIMEDIFF(date_t ime, now()) < '24:00:00';[/code]Ronald

      This is my query and i didn't get any data. What's wrong with this? (database has posiible values to display)
      [PHP]SELECT * FROM rainfall WHERE TIMEDIFF(Date, now()) < '24:00:00'[/PHP]

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        When your DATE column contains a date only, you'll get no results. Since you do a time operation with hours you also need the time in that column.
        With a date in there you can only do DATE operations.

        Ronald

        Comment

        • ghjk
          Contributor
          • Jan 2008
          • 250

          #5
          Originally posted by ronverdonk
          When your DATE column contains a date only, you'll get no results. Since you do a time operation with hours you also need the time in that column.
          With a date in there you can only do DATE operations.

          Ronald
          Thanks for your help. Now I put date time together. But getting all values in database.This is my query [PHP]SELECT * FROM rainfall WHERE TIMEDIFF(Date, now()) < '24:00:00'[/PHP]

          These are some values in database
          2008-04-06 19:30:00
          2008-03-24 09:30:00
          2008-04-05 12:30:00
          2008-04-06 17:30:00
          2008-04-07 15:30:34

          I can't find anything wrong.

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Works for me! Are you sure that the Date column has the DATETIME data type?

            Ronald

            Comment

            • ghjk
              Contributor
              • Jan 2008
              • 250

              #7
              Originally posted by ronverdonk
              Works for me! Are you sure that the Date column has the DATETIME data type?

              Ronald

              Yes.Type is datetime. Still i couldn't find anything wrong. :(

              Comment

              • ghjk
                Contributor
                • Jan 2008
                • 250

                #8
                Originally posted by ghjk
                Yes.Type is datetime. Still i couldn't find anything wrong. :(

                Solve it
                [PHP]SELECT * FROM rainfall WHERE TIMEDIFF('".$to day."',Date) < '24:00:00'[/PHP]

                Comment

                • ronverdonk
                  Recognized Expert Specialist
                  • Jul 2006
                  • 4259

                  #9
                  Ikay, it is solved then. See you around.

                  Ronald

                  Comment

                  Working...