存储过程在列名称上抛出错误

时间:2014-02-18 10:27:56

标签: mysql sql

delimiter $$

drop procedure if exists getEmptySeats $$
create procedure getEmptySeats(flight_number char(5),flight_date date)
begin
  select seatingID as Seats, concat(Seats.rowNumber, Seats.seatNumber) as Seat
  from Seating
  where
    aircraftID = aircraft_id and 
    seatID not in 
      ( Select seatID from Seating
        inner join Seats on Seating.seatID = Seats.seatID
        inner join Booking on Seating.bookingNumber = Booking.bookingNumber
        inner join Flights on Booking.flightCode = Flights.flightCode
        inner join FlightSchedule on Flights.flightNumber = FlightSchedule.flightNumber
        where flightNumber = flight_number and flightDate = flight_date)
order by seatingID;
end $$  

ORDER BY RAND()
LIMIT 1

delimiter ;

该程序假设在特定飞机上提供随机座位。 我不知道出了什么问题,我已经在互联网上搜索了这么久,并没有发现任何有用的东西。

当我调用该程序时,它给出了: Error Code: 1054. Unknown column 'Seats.rowNumber' in 'field list'

这是席位表:

seatID INT(11)
rowNumber TINYINT(4)
seatNumber CHAR(1)
seatPlacement VARCHAR(15)
plainID CHAR(6)

2 个答案:

答案 0 :(得分:1)

您需要将其与Seats表连接。用作子查询的那个无法直接访问

答案 1 :(得分:0)

正如@Madhivanan所提​​到的,你需要将它与Seats表连接起来。 还有一些需要修复的模糊列(除非你给表添加别名,否则前缀表名通常是最简单的答案)

delimiter $$

drop procedure if exists getEmptySeats $$
create procedure getEmptySeats(flight_number char(5),flight_date date)
begin
    select seatingID as Seats, concat(Seats.rowNumber, Seats.seatNumber) as Seat
        from Seating
        inner join Seats on Seating.seatID = Seats.seatID
        where
            aircraftID = aircraft_id and 
            seating.seatID not in 
              ( Select seating.seatID from Seating
                inner join Seats on Seating.seatID = Seats.seatID
                inner join Booking on Seating.bookingNumber = Booking.bookingNumber
                inner join Flights on Booking.flightCode = Flights.flightCode
                inner join FlightSchedule on Flights.flightNumber = FlightSchedule.flightNumber
                where Flights.flightNumber = flight_number and flightDate = flight_date)
        order by seatingID;
end $$  

ORDER BY RAND()
LIMIT 1

delimiter ;