With regards to the following statement:
SELECT *
FROM explorer.booking_record booking_record_
INNER JOIN explorer.client client_
ON booking_record_.labelno = client_.labelno
INNER JOIN explorer.tour_hotel tour_hotel_
ON tour_hotel_.tourcode = booking_record_.tourrefcode
INNER JOIN explorer.hotelrecord hotelrecord_
ON tour_hotel_.hotelcode = hotelrecord_.hotelref
WHERE booking_record_.bookingdate NOT LIKE '0000-00-00'
AND booking_record_.tourdeparturedate NOT LIKE '0000-00-00'
AND (hotelrecord_.hotelgroup = "LPL"
AND YEAR(booking_record_.tourdeparturedate)
BETWEEN YEAR(AddDate(Now(), Interval -5 Year))
AND YEAR(Now())
My MySQL skills are certainly not up to scratch, the actual result set I wish to find is “a customer who has been to 5 or more LPL hotels in the past 5 years“. So far I haven’t got as far as dealing with the count as I’m getting a huge number of results with some 250+ per customer.
I assume this is to do with the way I’m joining tables. Schema wise the booking_record table contains a tour reference code, which links to tour_hotel which then contains a hotelcode which links to hotelrecord. This hotelrecord table contains the hotelgroup.
The client table is joined to the booking_record via a booking reference and a client may have many bookings.
If anyone could suggest a way for me to do this I’d be very grateful and hopefully learn enough to do it myself next time! I’ve been scratching my head over this one for a few hours now!
Customers may have many bookings within booking_record
Daniel.
Every tour_hotel probably has more than one hotelrecord related to it. Thats why you get so many records. If you don’t need any information about the hotel you can just select what you need from client and use distinct.