给定客户端SQL的所有类型

时间:2012-12-10 22:30:05

标签: sql database oracle

好的,我有这样的数据库设置:

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

有人可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

我会使用having子句执行此操作。问题是:哪些客户端具有与RType表中相同数量的位置?这假设所有Location.Types都在此表中。

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

如果您确实需要,可以加入客户提供的其他信息。

答案 1 :(得分:0)

使用此:

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

答案 2 :(得分: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#