This is my view, I'm trying to output 3 columns called 'flightid', 'cancelledseats' and 'reservedseats'. But when I execute it, it executes too much data. I only want one row not 15.
CREATE VIEW check_flight_status AS
SELECT
(SELECT flightid FROM FLIGHTBOOKING WHERE flightid=111),
(SELECT COUNT(flightID) FROM FLIGHTBOOKING
WHERE status='c' AND flightid=111) AS cancelledseats,
(SELECT COUNT(flightid) FROM FLIGHTBOOKING
WHERE status='r' AND flightid=111) AS reservedseats
FROM flightbooking
INNER JOIN flight
ON flightbooking.flightid=flight.flightid;
(I'm not able to show you my tables without taking a screenshot because you'll not be able to understand it in formatted text as the layout messes up. However, I did try)
The expected output is to have 3 columns (flightid, cancelledseats and reservedseats) and one row with data 111,1,0.
EDIT
I've just fixed my problem!
CREATE VIEW check_flight_status AS
SELECT
(SELECT flightid FROM FLIGHTBOOKING WHERE flightid=111),
(SELECT COUNT(flightID) FROM FLIGHTBOOKING
WHERE status='c' AND flightid=111) AS cancelledseats,
(SELECT COUNT(flightid) FROM FLIGHTBOOKING
WHERE status='r' AND flightid=111) AS reservedseats
FROM flightbooking
INNER JOIN flight
ON flightbooking.flightid=flight.flightid
WHERE flight.flightid=111;
By adding WHERE flight.flightid=111; to the end, it only outputs one row. However, do you believe I'm over complicating my view? Could it be done simpler?
(SELECT flightid FROM FLIGHTBOOKING WHERE flightid=111)is useless and can be replaced with111