Search code examples
phpmysqlsqldatediff

Error: Every derived table must have its own alias


UPDATE `FlightSchedule`
SET delay=(
  SELECT *
  FROM (
    SELECT
      MINUTE(ETA - STA)
    FROM `FlightSchedule`
      WHERE `flightNum_arr` = '3517'
  )
)
WHERE `flightNum_arr` = '3517';

Says:

"Every derived table must have its own alias".

How to fix this issue?


Solution

  • Fixing it - exactly like it was shown in your error message:

    UPDATE `FlightSchedule` 
    SET delay=
    (SELECT update_field
     FROM 
     (
      SELECT MINUTE (ETA - STA) AS update_field
      FROM `FlightSchedule` 
      WHERE `flightNum_arr`='3517'
     ) AS internal_0
    )
    WHERE `flightNum_arr`='3517';
    

    But actually above there more correct suggestion - get rid of that nested subquery at all (see Gordon's answer).

    Edit (based on comments):

    If you want to find difference, use TIMEDIFF function:

    UPDATE `FlightSchedule` 
        SET delay = TIMEDIFF(ETA - STA)
        WHERE `flightNum_arr`='3517';