至於下面的語句:幫助與SQL查詢
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())
我的MySQL技能肯定沒有達到標準,實際的結果集我想找到的是「一個客戶誰一直到5個或更多LPL酒店在過去5年「。到目前爲止,我還沒有處理數量,因爲我得到了大量的結果,每個客戶有250多個。
我認爲這與我加入表格的方式有關。架構明智的booking_record表包含一個旅遊參考代碼,該代碼鏈接到tour_hotel然後包含一個酒店代碼鏈接到hotelrecord。此hotelrecord表包含hotelgroup。
客戶表通過預訂參考鏈接加入到booking_record,客戶可能會有很多預訂。
如果有人可以爲我提供一個方法來做到這一點,我會非常感激,並希望下次能夠自己學習!我一直在爲這個問題撓撓頭幾個小時吧!
客戶可能在booking_record內有很多預訂
Daniel。
這很好!任何想法現在我可以計數這些?我試着在最後添加一個「COUNT(booking_record_.labelno)> 5」,但結果是一行數爲109萬! – 2010-03-30 16:24:50
@djfrear,你可以嘗試將GROUP BY和HAVING結合起來。 – 2010-03-30 20:56:47