查询两次之间的数据选择?

时间:2016-03-08 22:30:04

标签: sql ms-access between

我正在使用Microsoft Access和SQL

我试图在两次之间选择记录:下午6:00:00和晚上11:59:00。

现在我有

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

但这不起作用,错误表明它错过了一个操作员。请帮忙

2 个答案:

答案 0 :(得分:1)

看起来你有两个表,只有两个不同的列。将它们全部放在一个表格中是一种更好的做法。但是,如果您确实希望保留表,那么这就是您的架构应该是什么样子

确保您的预订时间属于datetimetimetimestamps。在您的情况下,看起来您正在使用时间戳选择器。

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

CREATE TABLE seat_reserved(
  reservation_id INT,
  reservation_time TIMESTAMP
);

确保您将正确的数据插入表格。

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数据类型以24小时格式返回日期和时间。

要选择时间戳数据类型的时间部分,您可以使用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部分,因为它的语法不正确。

我创造了一个小提琴,让你玩

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

答案 1 :(得分:1)

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

这两个查询都会返回您需要的内容。问题是您需要where子句才能使SEAT_RESERVED.Reservation_Time不是时间戳。另一点需要指出的是将查询中指定的值转换为时间值。使用TimeValue函数可以将特定文本转换为时间值,从而可以在特定时间(包括分钟和秒)之间进行查询。

希望您觉得这很有帮助。