错误#1248 - 每个派生表必须有自己的别名

时间:2014-04-26 21:12:08

标签: mysql sql database

运行此查询时出现以下错误:错误#1248 - 每个派生表都必须有自己的别名

SELECT free_from, free_until
FROM (
SELECT a.end AS free_from,
(SELECT MIN(c.start)
 FROM bookings c
 WHERE c.start>a.end) as free_until
FROM bookings a
WHERE NOT EXISTS (
  SELECT 1
  FROM bookings b
  WHERE b.start BETWEEN a.end AND a.end + INTERVAL your_duration HOURS
)
AND a.end BETWEEN start_of_search_window AND end_of_search_window
)
ORDER BY free_until-free_from
LIMIT 0,3;

1 个答案:

答案 0 :(得分:0)

from子句中的每个子查询("派生表"当MySQL调用它们时)需要有一个名称。这是一个表别名。

如果格式化查询以使它们更具可读性,您可以很容易地看到需要别名的位置:

SELECT free_from, free_until
FROM (SELECT a.end AS free_from,
             (SELECT MIN(c.start)
              FROM bookings c
              WHERE c.start>a.end
             ) as free_until
      FROM bookings a
      WHERE NOT EXISTS (SELECT 1
                        FROM bookings b
                        WHERE b.start BETWEEN a.end AND a.end + INTERVAL your_duration HOURS
                       ) AND
            a.end BETWEEN start_of_search_window AND end_of_search_window
     ) t
-------^
ORDER BY free_until-free_from
LIMIT 0,3;