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

时间:2021-09-23 20:09:44

my first query returns me the value 20:

我的第一个查询返回值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'
     )

and my second query returns me 1:

我的第二个查询给我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

Now my problem is that I want to subtract the first query from the second query and give me the answer 19. how do i do that?

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

2 个解决方案

#1


0  

You could use sub sql to subtract

你可以使用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'
     )

#2


0  

you can do it in following way:

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

SELECT (query1) - (query2)

or

要么

SELECT (query1)  - (query2) AS Difference

or

要么

select @result = (query1) - (query2)

#1


0  

You could use sub sql to subtract

你可以使用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'
     )

#2


0  

you can do it in following way:

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

SELECT (query1) - (query2)

or

要么

SELECT (query1)  - (query2) AS Difference

or

要么

select @result = (query1) - (query2)