CREATE TABLE `reservation`
(
`name` VARCHAR(255),
`from` DATE,
`to` DATE
);
INSERT INTO RESERVATION VALUES ('aaa', '2013-04-23', '2013-05-06');
INSERT INTO RESERVATION VALUES ('bbb', '2013-04-01', '2013-08-06');
INSERT INTO RESERVATION VALUES ('ccc', '2013-05-12', '2013-05-19');
INSERT INTO RESERVATION VALUES ('ddd', '2013-06-01', '2013-07-27');
INSERT INTO RESERVATION VALUES ('eee', '2013-06-23', '2013-07-06');
INSERT INTO RESERVATION VALUES ('fff', '2013-07-23', '2013-08-09');
SELECT * FROM reservation WHERE 'from' <= '2013-07-01' AND 'to' >= '2013-07-01'
直播:http ://sqlfiddle.com/#!2/80d9cd/2
我想获得 2013-07 年期间的所有记录。这个查询应该返回我:
2 | bbb | 2013-04-01 | 2013-08-06
4 | ddd | 2013-06-01 | 2013-07-27
5 | eee | 2013-06-23 | 2013-07-06
6 | fff | 2013-07-23 | 2013-08-09
因为 2013-07 期间在此记录中是 > to 和 < from。为什么这会返回 0 条记录?如何使用 SQL 获取这些记录?