0

好的,所以我有一个这样的数据库设置:

Client {Clientt#, FirstName, LastName, Email, city};
RType {TName, Price, TRooms};
Location {Confirm#, Arrival, Departure, Origin, Client# (references Guest), Type (references RoomType)} 

我几乎需要创建 SQL 语句,为我提供所有类型的所有客户的列表。

我写了这个,但它不起作用:

SELECT Client#, FirstName, LastName, Type
FROM Client, Location
WHERE Client.Client# = Location.Client# and Location.Type = (select count(DISTINCT TName) from RType);

有人能帮我吗?

4

3 回答 3

1

我会使用该having子句来做到这一点。问题是:哪些客户端具有与 RType 表中相同数量的位置?这假定所有 Location.Types 都在此表中。

SELECT Client#
FROM Location l
group by Client#
having count(distinct l.type) = (select count(DISTINCT TName) from RType)

如果您确实需要,您可以加入来自客户的附加信息。

于 2012-12-10T22:38:08.737 回答
0

用这个:

SELECT Client#, FirstName, LastName, Type
FROM Client, Location
WHERE Client.Client# = Location.Client# and 
Location.Type IN (select DISTINCT TName from RType)
于 2012-12-10T22:37:37.123 回答
0

该 COUNT 函数返回您在其中拥有多少类型的计数,因此除非 Location.Type = 4,否则这将不起作用。删除 COUNT 或仅加入如下表:

SELECT t1.Client#, t1.FirstName, t1.LastName, t2.Type
FROM Client t1
    INNER JOIN Location t2
        ON t1.Client# = t2.Client#
于 2012-12-10T22:41:06.000 回答