1

我有以下数据库:

create table Hotel  (
   HNo char(4),
   Name varchar(20)   not null,
   Address varchar(50),
   Constraint PK_Hotel Primary Key (HNo))
)


create table Room  (
   RNo char(4),
   HNo char(4),
   Type char(6) not null,
   Price decimal (7,2),
   Constraint PK_Room Primary Key (HNo, RNo),
   Constraint FK_Room Foreign Key (HNo)
   references Hotel (HNo)
)


create table Guest  (
   GNo char(4),
   Name varchar(20) not null,
   Address varchar(50),
   Constraint PK_Guest Primary Key (GNo)

)


create table Booking   (
   HNo char(4),
   GNo char(4),
   DateFrom date,
   DateTo date,
   RNo char(4),
   Constraint PK_Booking Primary Key (HNo, GNo, DateFrom),
   Constraint FK_Booking Foreign Key (GNo)
   references Guest (GNo),
   Constraint FK_Booking_room Foreign Key (HNo, RNo)
   references Room (HNo, RNo),
   Constraint FK_Booking_hotel Foreign Key (HNo)
   references Hotel (HNo)
)

我正在努力使用dateto=> <=datefrom并让它工作。

对于 1997 年 3 月 26 日,我需要列出所有酒店中所有房间的详细信息,包括入住该房间的任何客人的姓名。我加入表格等并不太糟糕,但不确定如何在指定日期整体完成?有任何想法吗?

4

2 回答 2

1
SELECT
  h.Name,
  h.Address,
  r.RNo,
  r.HNo,
  r.Type,
  r.Price,
  G.Name,
  G.Address,
  ...
FROM Hotel         AS h
INNER JOIN Room    AS r  ON h.HNo = r.HNo
INNER JOIN Guest   AS g  ON r.RNo = g.RNo
INNER JOIN Booking AS b  ON b.GNo = g.GNo
                        AND b.HNo = h.HNo
WHERE b.DateFrom <= @DateFrom
  AND b.DateTo   => @DateTo;
于 2013-03-25T07:31:07.870 回答
1
  SELECT b.datefrom, b.dateto, h.name AS hotelname
         , b.rno
         , r.type
         , g.name AS guestname
    FROM booking b
    JOIN hotel h ON b.hno = h.hno
    JOIN guest g ON g.gno = b.gno
    JOIN room r ON b.rno = r.rno
   WHERE @yourDate BETWEEN b.datefrom AND b.dateto

编辑 :

@yourDate最后一行中的 替换为您的 DBMS 支持的合适表达式。

例如,在 Oracle 中,有一个函数TO_DATE可以将给定的字符串转换为日期,例如TO_DATE('26-MAR-1997') BETWEEN b.datefrom AND b.dateto. 在 SQL Server 中有CAST函数。

于 2013-03-25T07:31:37.170 回答