mysql使用between and處理時間區間包括右邊界

 datetime類型

CREATE TABLE `test_date` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;

‘create_time’爲datetime類型sql

select * from test_date where create_time BETWEEN '2018-10-16' AND '2018-10-28'

查詢出來的數據,包含‘2018-10-28 00:00:00 ’,不包括' 2018-10-28 01:00:00 'spa

若是是datetime的'2018-10-28' 會被轉成'2018-10-28 00:00:00'類型,因此查不出來2018-10-28的數據。3d

能夠經過精確到秒查code

select * from test_date where create_time BETWEEN '2018-10-16 00:00:00' AND '2018-10-28 23:59:59'

date類型

CREATE TABLE `test_date` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `create_time` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;

date類型blog

select * from test_date where create_time BETWEEN '2018-10-16' AND '2018-10-28'

相關文章
相關標籤/搜索