两次之间查询到select条数据?

Query to select data between two times?

我正在使用 Microsoft Access 和 SQL

我正在尝试 select 来自 table 两次之间的记录:6:00:00PM 和 11:59:00PM。

现在我有

SELECT RESERVATION.Reservation_ID, SEAT_RESERVED.Reservation_Time 
FROM RESERVATION 
INNER JOIN SEAT_RESERVED ON RESERVATION.Reservation_ID = SEAT_RESERVED.Reservation_ID 
WHERE timestamps SEAT_RESERVED.RESERVATION_TIME between ('6:00:00PM' and '11:59:00PM'); 

但这不起作用,错误提示缺少运算符。请帮助

看起来您有两个 table 并且只有 2 个不同的列。将它们全部放在一个 table 中是一个更好的做法。但是,如果您确实想保留 table,那么您的架构应该是这样的

确保您的预订时间类型为 datetimetimetimestamps。在您的情况下,您似乎正在使用时间戳 selector.

CREATE TABLE reservation(
  reservation_id INT PRIMARY KEY AUTO_INCREMENT,
  reservation_name VARCHAR(10)
);

CREATE TABLE seat_reserved(
  reservation_id INT,
  reservation_time TIMESTAMP
);

确保您将正确的数据插入 tables。

INSERT INTO reservation(reservation_name)
VALUES("George"), ("Roger"), ("John"), ("Lucas");

INSERT INTO seat_reserved(reservation_id, reservation_time)
VALUES(1, "2016-03-08 17:00:00"), 
      (2, "2016-03-08 18:00:00"), 
      (3, "2016-03-08 19:30:00"), 
      (4, "2016-03-08 12:00:00");

注意我们插入到 reservation_time 列中的值。

timestamps 数据类型 returns 24 小时格式的日期和时间。

要select时间戳数据类型的时间部分,可以使用TIME函数。

http://www.techonthenet.com/access/functions/date/time.php

SELECT RESERVATION.Reservation_ID, SEAT_RESERVED.Reservation_Time 
FROM RSERVATION
INNER JOIN SEAT_RESERVED ON RESERVATION.Reservation_ID = SEAT_RESERVED.Reservation_ID 
WHERE TimeValue(SEAT_RESERVED.RESERVATION_TIME) BETWEEN #18:00:00# AND #23:59:00#

我还删除了 WHERE 子句中的 timestamps 部分,因为那是不正确的语法。

我创建了一个 fiddle 给你玩

http://sqlfiddle.com/#!9/83bcf5/6/0

SELECT RESERVATION.Reservation_ID, SEAT_RESERVED.Reservation_Time 
FROM RESERVATION 
INNER JOIN SEAT_RESERVED ON RESERVATION.Reservation_ID = SEAT_RESERVED.Reservation_ID 
WHERE  TimeValue(SEAT_RESERVED.Reservation_Time)  >= TimeValue ('6:00:00PM') 
AND TimeValue(SEAT_RESERVED.Reservation_Time)  <= TimeValue ('11:59:00PM');

SELECT RESERVATION.Reservation_ID, SEAT_RESERVED.Reservation_Time 
FROM RESERVATION 
INNER JOIN SEAT_RESERVED ON RESERVATION.Reservation_ID = SEAT_RESERVED.Reservation_ID 
WHERE  TimeValue(SEAT_RESERVED.Reservation_Time)  BETWEEN  TimeValue ('6:00:00PM') 
AND TimeValue ('11:59:00PM');

这两个查询 return 您需要什么。问题是您需要 where 子句来使 SEAT_RESERVED.Reservation_Time 不是时间戳。另外需要指出的是将查询中指定的值转换为时间值。使用 TimeValue 函数可以将特定文本转换为时间值,从而可以在特定时间(包括分钟和秒)之间进行查询。

希望这对您有所帮助。