如何在sql中减去两个查询?

时间:2015-11-18 06:10:05

标签: sql database

我的第一个查询返回值20:

    SELECT numberofseats
    from plane
    where tail number in
     ( select flighttailnumberfk
       from flight
       where departuretime between '11/29/2014' and '11/30/2014' and
             flightdepartureairportfk = 'jfk' and
             flightarrivalairport = 'mli'
     )

我的第二个查询返回1:

    select count(reservationseatfk)
    from flight f, reservation r
    where f.departuretime between '11/29/2014' and '11/30/2014' and
          f.reservationflightidfk = f.flightid and r.reservationdeparturetimefk = f.departuretime

现在我的问题是我想从第二个查询中减去第一个查询并给我答案19.我该怎么做?

2 个答案:

答案 0 :(得分:2)

您可以使用sub sql来减去

SELECT numberofseats - ( select count(reservationseatfk)
    from flight f, reservation r
    where f.departuretime between '11/29/2014' and '11/30/2014' and
          f.reservationflightidfk = f.flightid and r.reservationdeparturetimefk = f.departuretime)
    from plane
    where tail number in
     ( select flighttailnumberfk
       from flight
       where departuretime between '11/29/2014' and '11/30/2014' and
             flightdepartureairportfk = 'jfk' and
             flightarrivalairport = 'mli'
     )

答案 1 :(得分:1)

你可以通过以下方式完成:

SELECT (query1) - (query2)

SELECT (query1)  - (query2) AS Difference

select @result = (query1) - (query2)