0

在加入、工会、ON-s 等方面,我不是很有经验,但我有一所学校,我需要从两张表中选择客户名称,贵宾和普通客户。

我尝试了两种方法,但不幸的是没有任何效果://1

SELECT Hotel_Rooms.Book_From, Hotel_Rooms.Book_To, Hotel_Rooms.[Book_To]-[Book_From] AS Booked_Days, Hotel_Rooms.[Price_Per_Night]*[Booked_Days] AS Price, Reg_Customers.Cust_name
FROM Reg_Customers
UNION
SELECT Hotel_Rooms.Book_From, Hotel_Rooms.Book_To, Hotel_Rooms.[Book_To]-[Book_From] AS Booked_Days, Hotel_Rooms.[Price_Per_Night]*[Booked_Days] AS Price, Vip_Customers.Cust_name
FROM Vip_Customers
INNER JOIN Hotel_Rooms ON Reg_Customers.Cust_ID = Hotel_Rooms.Cust_ID && Vip_Customers.Cust_ID = Hotel_Rooms.Cust_ID
ORDER BY Hotel_Rooms.[Price_Per_Night]*[Booked_Days] DESC;

//2

SELECT Hotel_Rooms.Book_From, Hotel_Rooms.Book_To, Hotel_Rooms.[Book_To]-[Book_From] AS Booked_Days, Hotel_Rooms.[Price_Per_Night]*[Booked_Days] AS Price, Reg_Customers.Cust_name, Vip_Customers.Cust_name
FROM Reg_Customers, Vip_Customers
INNER JOIN Hotel_Rooms ON Reg_Customers.Cust_ID = Hotel_Rooms.Cust_ID && Vip_Customers.Cust_ID = Hotel_Rooms.Cust_ID
ORDER BY Hotel_Rooms.[Price_Per_Night]*[Booked_Days] DESC;

没有一个查询可以正确选择、连接表。你能帮忙吗?表格方案,如果这些有帮助

房间 { Rooms_ID, Cust_ID, Room_Capacity, Available, Book_Time, Book_Price_Per_Night, Book_From, Book_To }

普通客户 {Cust_ID, Cust_Name, Cust_Phone, Cust Card} vip {Cust_ID, Cust_Name, Cust_Phone, Cust Card}

谢谢你。如果您可以提供一个有效的查询,无论是#1 还是#2 版本,那就太好了,谢谢。

4

2 回答 2

0

不知道您的数据库有点棘手,但是根据您提供的内容,此联合/排序可能会起作用

{ 
    SELECT hr.Book_From, hr.Book_To, hr.[Book_To]-[Book_From] AS Booked_Days, hr.[Price_Per_Night]*[Booked_Days] AS Price, rc.Cust_name
    FROM Reg_Customers rc
    LEFT JOIN Hotel_Rooms AS hr ON rc.Cust_ID = hr.Cust_ID
} UNION {
    SELECT hr.Book_From, hr.Book_To, hr.[Book_To]-[Book_From] AS Booked_Days, hr.[Price_Per_Night]*[Booked_Days] AS Price, vc.Cust_name
    FROM Vip_Customers vc
    LEFT JOIN Hotel_Rooms AS hr ON vc.Cust_ID = hr.Cust_ID
}
ORDER BY [Price_Per_Night]*[Booked_Days] DESC;
于 2013-04-20T23:05:27.293 回答
0

你的第二种方法更好,但你犯了一个错误。您可以像这样连接表:

from table1, table2
where table1.id = table2.id

或像这样(哪个更好)

from table1 join table2 on table1.id = table2.id

但您不能在同一个查询中同时使用这两种技术。这就是您在第二个示例中的内容。

编辑从这里开始

再次阅读您的问题,联合查询是一种更好的方法。但是,您在这里犯了两个错误:

FROM Vip_Customers
INNER JOIN Hotel_Rooms ON Reg_Customers.Cust_ID = Hotel_Rooms.Cust_ID 
&& Vip_Customers.Cust_ID = Hotel_Rooms.Cust_ID
ORDER BY Hotel_Rooms.[Price_Per_Night]*[Booked_Days] DESC;

第一个错误是在逻辑上。您从 vip 客户中选择,但从普通客户表中加入。第二个错误是语法。将“&&”替换为“和”。据我所知,没有任何 RDBMS 支持“&&”。

于 2013-04-20T22:47:48.217 回答