我想从特定表中提取数据。然而,约束是我需要 2 个关系视图来获取数据。我可以在不使用视图的情况下获取数据,但要求是使用视图。我是 db 的新手。我希望你们能帮助我。因此,我认为我需要使用 2 个表中的数据。但据我了解,我认为我不需要使用 2 张桌子。希望你们能给点建议。正如我之前所说,我需要使用 2 个视图来提取数据。
FlightTripNo Departure Destination FinalDestination
- - - - - - - - - - - - - - - -
05 Singapore Bangalore Chennai
03 Sdyney Auckland
这是下表。
FLIGHTTRIP( FlightTripNo,PlaneNo)
PRIMARY KEY (FlightTripNo),
( 1, JET348)
( 2, TR1313)
( 3, TR287)
( 4, MY871P)
( 5, SQ777)
( 6, AI322X)
飞行停止表
FLIGHTSTOP( FlightTripNo, FlightStopNo, DEPARTURE, DESTINATION )
PRIMARY KEY (FlightTripNo, FlightStopNo)
UNIQUE UNIQUE(FlightTripNo, DEPARTURE, DESTINATION)
FOREIGN KEY (FlightTripNo) REFERENCES FLIGHTTRIP(FlightTripNo)
INSERT INTO FLIGHTSTOP VALUES( 1, 1, 'Singapore', 'Malaysia');
INSERT INTO FLIGHTSTOP VALUES( 1, 2, 'Malaysia', 'Chennai');
INSERT INTO FLIGHTSTOP VALUES( 1, 3, 'Chennai', 'Bangalore');
INSERT INTO FLIGHTSTOP VALUES( 3, 1, 'Syndey', 'Auckland');
INSERT INTO FLIGHTSTOP VALUES( 5, 1, 'Singapore', 'Bangalore');
INSERT INTO FLIGHTSTOP VALUES( 5, 2, 'Bangalore', 'Chennai');
......
Total 400 records
这是我在不使用视图的情况下做了多远。但我需要使用视图。和 2 视图。请指教。
SELECT FLIGHTSTOP.FlightTripNo,FLIGHTSTOP.FlightStopNo, FLIGHTSTOP.DEPARTURE AS ORIGIN, FLIGHTSTOP.DESTINATION AS DESTINATION1, T2.DESTINATION AS DESTINATION2
FROM FLIGHTSTOP LEFT JOIN FLIGHTSTOP T2 ON FLIGHTSTOP.FlightTripNo = T2.FlightTripNo AND FLIGHTSTOP.DEPARTURE = T2.DEPARTURE
WHERE FLIGHTSTOP.FlightTripNo IN (SELECT FlightTripNo FROM FLIGHTSTOP
GROUP BY FlightTripNo
HAVING COUNT(*) < 3)
ORDER BY FLIGHTSTOP.FlightTripNo;