MySQL Complex Queries -



MySQL Complex Queries -

hey i've been killing myself trying figure out how these queries. can help me out. these tables have currently.

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 cost

and these queries need do.

-list guests have bookings (past , present) in same hotel.

-create view vip-guest lists guests have reservations 4 star hotels or 4 star hotels

-among vips find invitee largest total remain (in term of number of days). express query view , without view

can help me out?

this should started. post on stackoverflow, need come specific questions or errors or problems. query posted in comments top.... question in itself: "i have these tables, 1 specific goal (question/result set), , tried query... gives me result or gives me error."

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, cost

all guests , bookings...

-- guests: select * guest; -- bookings: select * booking; select * invitee bring together booking on guest.guest_no = booking.guest_no; -- same as... select * guest, booking guest.guest_no = booking.guest_no; -- and... comments query missing grouping clause select guest_no, guest_name, count(*) booking_count invitee bring together booking on guest.guest_no = booking.guest_no grouping guest_no, guest_name; select guest_no, guest_name, count(distinct hotel_no) hotel_count invitee bring together booking on guest.guest_no = booking.guest_no grouping guest_no, guest_name having count(distinct hotel_no) = 1;

and count(distinct hotel_no) because... might have 3 bookings @ hotel , 1 @ hotel b. basic bring together give me 4 rows person. don't care how many bookings. care how many hotels. want count distinct occurrences of hotel_no per person (there's grouping by) instead of every row.

guests stars...

-- have invitee , hotel joined. bc hotel has stars. -- booking has hotel_no. so... can utilize lastly query , -- bring together in hotel star information. in -- want set filter number of stars -- looking =4 or >=4 or that. -- might want check out distinct list of names -- instead of row each booking.

number of days stayed...

-- utilize sec query. -- datediff(date_to, date_from) days_stay gives length of remain -- don't know view is. -- top length go 2 ways... either order , limit if there -- 1 person top length (let's 10 days). if there many people -- have stayed 10 days, you'll need max on days_stay , either bring together -- in or utilize in nested select.

this assumes there single highest length of stay. 1 person stayed 10 days.

select guest_no, guest_name, datediff(date_to, date_from) days_stayed vip_guest bring together booking on vip_guest.guest_no = booking.guest_no order datediff(date_to, date_from) desc limit 1,1

this should work many... (i'm not testing these... kind of looking @ it)

select distinct guest_no, guest_name, datediff(date_to, date_from) max_stay vip_guest bring together booking on vip_guest.guest_no = booking.guest_no datediff(date_to, date_from) = ( select max(datediff(date_to, date_from)) days_stayed booking )

the nested query gets maximum remain length of everyone. vip_guest , bookings joined give invitee , date imfo. bookings every vip_guest. want filter downwards remain lengths == max remain length. in case person had multiple 10 day stays (my arbitrary max remain length)... utilize distinct.

now... thats point nested query. don't know in view. possible none of max vip guests had remain long max remain length. in case, query homecoming nothing.

mysql

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -