0
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 获取这些记录?

4

2 回答 2

4

您的列名不应该用引号引起来。它们应该在刻度中:

SELECT * 
FROM reservation 
WHERE `from` <= '2013-06-01' 
AND `to` >= '2013-06-01'

http://sqlfiddle.com/#!2/80d9cd/3

于 2013-06-16T19:09:21.587 回答
1

这里是工作的

http://sqlfiddle.com/#!2/80d9cd/5

你有from并且to是保留的mysql关键字,你应该用反引号括起来

于 2013-06-16T19:10:09.143 回答