Select data between a date/time range

后端 未结 8 1619
迷失自我
迷失自我 2020-11-27 14:43

How do I select data between a date range in MySQL. My datetime column is in 24-hour zulu time format.

select * from hockey_stats 
where game_d         


        
相关标签:
8条回答
  • 2020-11-27 15:14

    In a simple way it can be queried as

    select * from hockey_stats 
    where game_date between '2018-01-01' and '2018-01-31';
    

    This works if time is not a concern.

    Considering time also follow in the following way:

    select * from hockey_stats where (game_date between '2018-02-05 01:20:00' and '2018-02-05 03:50:00');
    

    Note this is for MySQL server.

    0 讨论(0)
  • 2020-11-27 15:16

    A simple way :

    select  * from  hockey_stats 
    where  game_date >= '2012-03-11' and game_date  <= '2012-05-11'
    
    0 讨论(0)
  • 2020-11-27 15:16

    MySQL date format is this : Y-M-D. You are using Y/M/D. That's is wrong. modify your query.

    If you insert the date like Y/M/D, It will be insert null value in the database.

    If you are using PHP and date you are getting from the form is like this Y/M/D, you can replace this with using the statement .

    out_date=date('Y-m-d', strtotime(str_replace('/', '-', $data["input_date"])))
    
    0 讨论(0)
  • 2020-11-27 15:17

    You must search date defend on how you insert that game_date data on your database.. for example if you inserted date value on long date or short.

    SELECT * FROM hockey_stats WHERE game_date >= "6/11/2018" AND game_date <= "6/17/2018"
    

    You can also use BETWEEN:

    SELECT * FROM hockey_stats WHERE game_date BETWEEN "6/11/2018" AND "6/17/2018"
    

    simple as that.

    0 讨论(0)
  • 2020-11-27 15:24

    You probably need to use STR_TO_DATE function:

    select * from hockey_stats 
    where
      game_date between STR_TO_DATE('11/3/2012 00:00:00', '%c/%e/%Y %H:%i:%s')
                    and STR_TO_DATE('11/5/2012 23:59:00', '%c/%e/%Y %H:%i:%s') 
    order by game_date desc;
    

    (if game_date is a string, you might need to use STR_TO_DATE on it)

    0 讨论(0)
  • 2020-11-27 15:25

    You need to update the date format:

    select * from hockey_stats 
    where game_date between '2012-03-11 00:00:00' and '2012-05-11 23:59:00' 
    order by game_date desc;
    
    0 讨论(0)
提交回复
热议问题