简体   繁体   中英

Select data between datetime range

I have a table called lap and it contains data like this :

code | name | details | time_stamp
===================================
1a   | A-wan| Goods   | 01-02-2019 11:08:10
3a   | D-wan| Goods   | 05-02-2019 01:00:40
2a   | B-gud| Foods   | 02-03-2019 05:31:05
1a   | A-cro| Bills   | 02-03-2019 15:30:15
1a   | A-set| Bills   | 03-03-2019 17:24:35
1a   | C-won| Goods   | 03-03-2019 10:21:55
1a   | C-gud| Foods   | 04-03-2019 01:30:06

I used dd-mm-yy hh:mm:ss format

and my query SELECT * FROM lap WHERE time_stamp >= '02-03-2019 00:00:00' AND time_stamp <= '03-03-2019 23:59:59'

the result is

code | name | details | time_stamp
===================================
3a   | D-wan| Goods   | 05-02-2019 01:00:40
2a   | B-gud| Foods   | 02-03-2019 05:31:05
1a   | A-cro| Bills   | 02-03-2019 15:30:15
1a   | A-set| Bills   | 03-03-2019 17:24:35
1a   | C-won| Goods   | 03-03-2019 10:21:55

it includes the data of 3a | D-wan| Goods | 05-02-2019 01:00:403a | D-wan| Goods | 05-02-2019 01:00:40 3a | D-wan| Goods | 05-02-2019 01:00:40 i think it's may be because of 05 is bigger than 02 although it's different month. Anyone can help this?

when i change the query on the same date it works well SELECT * FROM lap WHERE time_stamp >= '02-03-2019 00:00:00' AND time_stamp < '02-03-2019 23:59:59'

code | name | details | time_stamp
===================================
2a   | B-gud| Foods   | 02-03-2019 05:31:05
1a   | A-cro| Bills   | 02-03-2019 15:30:15

You should use date function then it will work,

SELECT * FROM lap WHERE time_stamp BETWEEN '02-03-2019' AND '03-03-2019';

or you can change the format to yyyy-mm-dd into your backend and check

SELECT * FROM lap WHERE time_stamp BETWEEN '2019-03-02' AND '2019-03-03';

try

 SELECT * FROM lap WHERE time_stamp BETWEEN DATE_FORMAT('02-03-2019 00:00:00', '%d-%m-%Y %h:%m:%s') AND DATE_FORMAT('03-03-2019 23:59:59', '%d-%m-%Y %h:%m:%s');

for mysql date time format see link bellow https://dev.mysql.com/doc/refman/8.0/en/datetime.html

If you pass the date in another format its better you change the format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM