2017-04-14 200 views
0

我一直在尋找無處不在,這將是最好的網站去請求幫助。我必須進行SQL查詢,檢查特定航班的預訂總量,然後根據系統應該提供的飛機選擇的預訂數量。第一個查詢的作品,它發現預訂的總數,我認爲我有案例說明選擇一架飛機的權利,但我無法找到實際加入兩個查詢的方式,我試圖使用統一,內部連接和嵌套查詢,但它似乎預訂的席位總數(第一個查詢的答案)無法找到,請幫助我們。基於第一個查詢答案加入兩個SQL查詢

首先SQL查詢(找到預訂的總數)

SELECT count(bookingdetails.FlightID)AS TotalNumberOfSeatsBooked,flightdetails.FlightID 
FROM bookingdetails, bookingdetails AS TEMP,flightdetails 
WHERE bookingdetails.BookingID = TEMP.BookingID 
AND bookingdetails.FlightID= flightdetails.FlightID 
Group BY FlightID; 

第二個SQL查詢(取決於有多少訂單是由選擇的機型)

SELECT CASE chooseaircraft 
    WHEN TotalNumberOfSeatsBooked <= 110 THEN 'BA 146-200' 
    ELSE'Embraer 170' 
    END AS ChoiceOfAircraft 
    FROM aircrafttype; 

非常感謝大家 後一個答案,我認爲即時通過將兩個查詢合併到正確的方向,代碼現在顯示子查詢中的座位和航班號的總數,但飛機列的選擇仍然不顯示,但如果您運行通過它的自我查詢我知道我接近得到這一點,我希望得到任何幫助,成爲更好的SQL代碼,我現在是:

SELECT count(bookingdetails.FlightID)AS TotalNumberOfSeatsBooked,flightdetails.FlightID 
FROM bookingdetails, bookingdetails AS TEMP,flightdetails 
WHERE bookingdetails.BookingID = TEMP.BookingID 
AND bookingdetails.FlightID= flightdetails.FlightID 
AND bookingdetails.FlightID= flightdetails.FlightID IN(
SELECT CASE WHEN count(bookingdetails.FlightID) <= 110 THEN 'BA 146-200' 
ELSE'Embraer 170' 
END AS ChoiceOfAircraft 
FROM bookingdetails,flightdetails) 
Group BY FlightID; 

回答

0

您可以使用您的CASE聲明相同的表達count(bookingdetails.FlightID)(或)將第一個查詢包裝在子查詢中並訪問外部查詢中的列。也就是說

CASE WHEN count(bookingdetails.FlightID) <= 110 THEN 'BA 146-200' 
ELSE'Embraer 170' 
END AS ChoiceOfAircraft 
+0

謝謝老兄我在哪裏錯了就加入查詢任何想法,我已經更新了我上面的代碼將是巨大的有這對我的學位項目的工作:) –