嘿,我一直在试图弄清楚如何进行这些查询。有人可以帮我吗。 这些是我目前的表格。
BOOKING
HOTEL_NO
GUEST_NO
DATE_FROM
DATE_TO
ROOM_NO
GUEST
GUEST_NO
客人姓名
CITY
地址
ZIP_CODE
HOTEL
HOTEL_NO
HOTEL_NAME
CITY
地址
ZIP_CODE
STAR
ROOM
ROOM_NO
HOTEL_NO
房型
价格
这些是我需要做的查询。
- 在同一家酒店预订所有预订(过去和现在)的客人。
创建一个VIP-Guest视图,其中列出了仅预订4星级酒店的客人 4星级酒店
- 在VIP中找到总逗留时间最长的客人(按天数计算)。 将此表达为具有视图且没有视图的查询
有人能帮助我吗?
答案 0 :(得分:2)
这应该让你开始。要发布stackoverflow,您需要提出具体问题或错误或问题。就像你在评论中发布的查询一样....这可能是一个问题:“我有这些表,这个特定的目标(问题/结果集),我试过这个查询..它给了我这个结果,或者它给了我这个错误。“
BOOKING: HOTEL_NO, GUEST_NO, DATE_FROM, DATE_TO, ROOM_NO
GUEST: GUEST_NO, GUEST_NAME, CITY, ADDRESS, ZIP_CODE
HOTEL: HOTEL_NO, HOTEL_NAME, CITY, ADDRESS, ZIP_CODE, STAR
ROOM: ROOM_NO, HOTEL_NO, ROOM_TYPE, PRICE
所有客人和预订......
-- all guests: select * from guest;
-- all bookings: select * from booking;
select *
from guest
join booking on guest.guest_no = booking.guest_no;
-- which is the same as...
select *
from guest, booking
where guest.guest_no = booking.guest_no;
-- and... your comments query was missing a group by clause
select guest_no, guest_name, count(*) as booking_count
from guest
join booking on guest.guest_no = booking.guest_no
group by guest_no, guest_name;
select guest_no, guest_name, count(distinct hotel_no) as hotel_count
from guest
join booking on guest.guest_no = booking.guest_no
group by guest_no, guest_name
having count(distinct hotel_no) = 1;
和我count(distinct hotel_no)
因为......他们可能在A酒店预订3次,在B酒店预订1次。基本的加入会为我提供4行预订。我不在乎预订多少。我在乎多少家酒店。所以我想计算每人hotel_no
的不同出现次数(有那个分组)而不是每一行。
来自他们明星的客人......
-- so we have to get guest and hotel joined. bc hotel has stars.
-- booking has hotel_no. so... we can use that last query and
-- join in HOTEL to get the star information. in the WHERE you
-- will want to put your filter for the number of stars that you
-- are looking for =4 or >=4 or something like that.
-- you might want to check out DISTINCT to get just a list of names
-- instead of a row for each booking.
他们住的天数......
-- use the second query.
-- datediff(date_to, date_from) as days_stay gives you the length of stay
-- i don't know what the view is.
-- to get the top length could go two ways... either ORDER BY and LIMIT if there is
-- only one person with the top length (let's say 10 days). if there are many people
-- who have stayed 10 days, you'll need to do a MAX on the days_stay and either join
-- that in or use it in the WHERE as a nested select.
这假设有一个最长的逗留时间。只有一个人住了10天。
SELECT guest_no, guest_name, datediff(date_to, date_from) days_stayed
FROM vip_guest
join booking on vip_guest.guest_no = booking.guest_no
order by datediff(date_to, date_from) desc
limit 1,1
这应该适用于许多人......(我不测试这些...只是看看它)
SELECT distinct guest_no, guest_name, datediff(date_to, date_from) max_stay
FROM vip_guest
join booking on vip_guest.guest_no = booking.guest_no
where datediff(date_to, date_from) = (
select max(datediff(date_to, date_from)) as days_stayed
from booking )
嵌套查询获取每个人的最长逗留时间。 vip_guest和预订加在一起给我们客人和日期imfo。我们将为每个vip_guest获取所有预订。所以我们希望将其过滤到保持长度==最大停留长度的位置。如果一个人有10天多次停留(我的任意最长停留时间长度)...使用不同的。
现在......这是关于嵌套查询的一个好点。我不知道你的观点是什么。只要最长逗留时间,最大贵宾客人都不可能住宿。在这种情况下,此查询将不返回任何内容。