這應該讓你開始。發佈在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周的預訂和1酒店B.基本的加入會給我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天的逗留(我的任意最大逗留長度)...使用不同的。
現在...這是一個關於嵌套查詢的好點。我不知道你的觀點是什麼。有可能沒有最大VIP客人逗留,只要最長停留時間。在這種情況下,此查詢將不會返回任何內容。
究竟是什麼問題?不要說「我不知道該怎麼做」 - [so]是__not__免費的編碼服務。 – 2014-10-19 19:34:50
這就是我爲第一個問題所嘗試的。 – richard 2014-10-19 19:35:47
SELECT G.GUEST_NAME,COUNT(B.HOTEL_NO) FROM GUEST G,BOOKING B WHERE G.GUEST_NO = B.GUEST_NO HAVING COUNT(*)= 1; ....這工作,但我不知道爲什麼它不給我我想要的。 – richard 2014-10-19 19:36:27