MySQL Datetime Query Help

Discussion in 'MySQL' started by T0PS3O, Nov 30, 2006.

  1. #1
    Esteemed MySQL Legends,

    I have a bunch of timestamps in a datetime type column like this: "2005-11-12 21:15:27"

    I have about 10K rows and want to know how many are in each hour of the day. I.e. how many between 21:00 and 22:00 like the example above.

    Is there any way of doing this with a simple query that doesn't involve PHP then parsing the results?

    Something like SELECT COUNT(time_stamp) FROM table WHERE time_stamp BETWEEN "hour" 21 AND "hour" 22

    Any help would be greatly appreciated!

    PS Can't do sub-queries since it's MySQL 3.X
     
    T0PS3O, Nov 30, 2006 IP
  2. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Not sure if this will work but :
    You can do something like
    SELECT count(*)
    FROM table
    where date_part('hour', timestamp) in (10, 11)

    Edit - not sure if it works with MySQL
     
    maiahost, Nov 30, 2006 IP
    T0PS3O likes this.
  3. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks but it's not liking it. From which Db Engine did you get that?
     
    T0PS3O, Nov 30, 2006 IP
  4. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That's usually for PostgreSQL but MySQL has a similar finction ... the search continues
     
    maiahost, Nov 30, 2006 IP
  5. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #5
    T0PS3O, Nov 30, 2006 IP
  6. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I was just about to suggest another way with substr but I see that you did it :)
     
    maiahost, Nov 30, 2006 IP
  7. fatmagoo

    fatmagoo Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    lol, that's almost as old as I am - time for an upgrade perhaps? :)
     
    fatmagoo, Nov 30, 2006 IP
  8. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Not until RHEL 3 supports all of it in RPMs.

    And I've never had anything I couldn't do with MySQL 3 - it just takes a few more lines of code every now and then.
     
    T0PS3O, Nov 30, 2006 IP