简体   繁体   English

根据第一个查询答案联接两个SQL查询

[英]Join two SQL Queries based on first query answer

I have been looking everywhere and this would be the best website to ask someone for the help. 我一直到处寻找,这将是向某人寻求帮助的最佳网站。 I have to make SQL query that checks the total amount of bookings for the specific flight and then based on the number of bookings the system should provide the choice of an aircraft. 我必须进行SQL查询,以检查特定航班的预订总额,然后根据预订数量,系统应提供飞机的选择。 First query works and it finds total number of bookings and i think i have the case statement right to choose an aircraft but i cant find the way of physically joining both queries , i tried to use unison , inner join and nested queries but it appears that Total number of seats booked (the answer from first query ) cannot be found please help me guys. 第一个查询有效,它可以找到预订总数,而且我认为我有选择飞机的案例陈述权,但是我找不到物理上同时连接这两个查询的方式,我尝试使用统一,内部连接和嵌套查询,但看来找不到预订的座位总数(第一个查询的答案),请帮帮我。

First SQL Query(find total number of bookings ) 第一个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;

SECOND SQL Query(Choose an aircraft type depending on how many bookings are made) 第二个SQL查询(根据预订数量选择飞机类​​型)

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

Big Thanks to everyone After one answer i think im heading in the right direction with merging the both queries together , the code now displays the total number of seats and flight number in the sub query but the choice of aircraft column still doesnt show but it does if you run the query by it self i know i am close to getting this and i would appreciate any help to become better in SQL the code i have now is : 谢谢大家。在我回答一个问题后,我认为我正朝着正确的方向进行合并两个查询,该代码现在在子查询中显示了座位总数和航班号,但飞机列的选择仍然没有显示,但确实如此如果您自行运行查询,我就知道我已经接近完成了,我将感谢您提供任何帮助以改善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;

You can use the same expression count(bookingdetails.FlightID) in your CASE statement (or) wrap your first query in a subquery and access the column in your outer query. 您可以在CASE语句中使用相同的表达式count(bookingdetails.FlightID) (或)将第一个查询包装在子查询中,并访问外部查询中的列。 That is 那是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM