添加计数功能

时间:2015-03-09 03:32:08

标签: sql database ms-access-2010

以下是我们正在制作的表格。

HOTEL (HotelNo, HotelName, City)

ROOM_TYPE (RoomType, Descr, RoomRate)

ROOM (HotelNo, RoomNo, RoomType, PhoneExt)
FK1: Foreign key HotelNo references HOTEL
FK2: Foreign key RoomType references ROOM_TYPE
GUEST (GuestNo, FirstName, LastName, Address, City, State, ZipCode)
GUEST_PHONE (PhoneNumber, GuestNo, PhoneType)
FK: Foreign key GuestNo references GUEST

RESERVATION (ResNum, HotelNo, RoomNo, GuestNo, ArrivalDate, DepartureDate, NumPersons)
FK1: Foreign Key (HotelNo, RoomNo) references ROOM
FK2: Foreign Key GuestNo references GUEST

按名称列出客人,并在每家酒店预订客房的次数。按照从最常见到最不常见的客人的顺序排列列表。

这是我的SQL语句,它会检索出现GuestNo的预约人员的所有姓名。我不知道在哪里加入Count函数。

SELECT GUEST.FirstName,  RESERVATION.GuestNo
FROM GUEST 
INNER JOIN RESERVATION ON GUEST.GuestNo = RESERVATION.GuestNo
ORDER BY GUEST.FirstName

1 个答案:

答案 0 :(得分:0)

我认为你想要的是什么,你需要使用聚合函数(Count)以及" Group By"这将给你实际的结果。

我可能误解了你的例子,但根据我能够收集的信息,它应该看起来像这样:

SELECT COUNT(RESERVATION.GuestNo), GUEST.FirstName, RESERVATION.GuestNo

FROM GUEST INNER JOIN RESERVATION on GUEST.GuestNo = RESERVATION.GuestNo

GROUP BY GUEST.FirstName

编辑:我认为您希望按来宾分组,而不是预订#。