我想在查询中输入不同表中两列之间的减法,它一直在说错误......
SELECT FlightDate,
Plane,
Destination,
Capacity
FROM
FlightSchedule,
Routes,
Aircrafts
WHERE
FlightSchedule.RID=Routes.RouteID
AND FlightSchedule.Plane=Aircrafts.AcID
AND (SELECT SUM(Capacity)
FROM Aircrafts)
-
(SELECT Class , count(*)
FROM Tickets);
答案 0 :(得分:0)
多个级别的查询错误。
首先,您不能将SELECT容量的详细信息作为WHERE下的子查询。 WHERE子句用于条件参数。
试试这个:
WITH VACANCY as (SELECT SUM(Capacity) FROM Aircrafts) - (SELECT Count(*) FROM Tickets)
SELECT FlightDate,Plane, Destination, Vacancy
FROM FlightSchedule,Routes,Aircrafts
WHERE FlightSchedule.RID=Routes.RouteID AND FlightSchedule.Plane=Aircrafts.AcID;
你还需要一个条件选择票数,但我不知道票的架构所以......
答案 1 :(得分:0)
我会把它分成两部分。首先获取所需的数据,然后执行数学运算。您还使用旧的SQL语法。您应该使用新的JOIN语法重新格式化。它也更容易阅读。
1宣布一张表格,为您提供航班时刻表和飞机信息。
Declare @AIRCRAFTCAPACITY Table
(
[FlightDate] [datetime] NOT NULL,
[Plane] [varchar](50) NOT NULL,
[Destination] [varchar](50) NOT NULL,
[Capacity] [INT] NOT NULL,
[NoOfSeatsRemaining] [INT] NULL
);
然后插入您需要的数据。
INSERT @AIRCRAFTCAPACITY
(
[FlightDate],
[Plane],
[Destination],
[Capacity]
)
SELECT FS.FlightDate,
A.Plane,
FS.Destination,
A.Capacity
FROM
FlightSchedule FS
INNER
JOIN Routes R
ON
FS.RID = R.RouteID
INNER
JOIN Aircrafts A
ON
FS.Plane = A.AcID
现在执行数学计算剩余容量。我已假设您正在为特定路线执行此操作。但我相信你可以相应地调整你的SQL。
UPDATE @AIRCRAFTCAPACITY
SET
[NoOfSeatsRemaining] = [Capacity] - T.TICKETS_SOLD
FROM
@AIRCRAFTCAPACITY A
INNER
JOIN
(
SELECT ROUTEID, COUNT(ROUTEID) AS TICKETS_SOLD
FROM
Tickets T1
WHERE
T1.ROUTEID = A.ROUTEID
GROUP
BY ROUTEID
) T
ON
A.ROUTEID = T.ROUTEID
Apolgies如果语法稍微偏离,因为当你没有基础表时很难构造SQL。
但希望它会有所帮助。