在日期时间范围之间选择数据

时间:2019-03-26 07:59:22

标签: php mysql datetime range between

我有一个名为lap的表,它包含这样的数据:

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

我使用了dd-mm-yy hh:mm:ss格式

和我的查询 SELECT * FROM lap WHERE time_stamp >= '02-03-2019 00:00:00' AND time_stamp <= '03-03-2019 23:59:59'

结果是

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

包含以下数据  3a | D-wan| Goods | 05-02-2019 01:00:40 我认为可能是因为0502大,尽管月份不同。 有人可以帮忙吗?

当我在同一日期更改查询时,效果很好 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

2 个答案:

答案 0 :(得分:0)

您应该使用date函数,它将起作用,

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

或者您可以将格式更改为yyyy-mm-dd到您的后端并检查

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

答案 1 :(得分:0)

尝试

 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');

有关mysql日期时间格式,请参见下面的链接 https://dev.mysql.com/doc/refman/8.0/en/datetime.html

如果以其他格式传递日期,则最好更改格式。