0

我有这个 SQL 请求,它应该将两个子查询的结果与 合并INNER JOIN,但我得到一个错误,我不知道为什么。请问你能帮帮我吗?

SELECT 
      dateT, PC.id_serie
FROM (
      SELECT id_serie, MIN(dateF) as dateF FROM fixings GROUP BY id_serie
) AS PC1
INNER JOIN
(
      SELECT target_calendar.dateT, series.id_serie FROM target_calendar, series  LEFT JOIN fixings ON (PC.DateT=fixings.dateF) AND (PC.id_serie = fixings.id_serie)
) AS PC
ON
      PC1.dateF = PC.dateT
AND 
      (((pc1.[dateF]) Is Null))
AND 
      DateT <= Date();

错误:由别名“DateF”引起的循环引用

4

2 回答 2

1

事实上,答案是添加更多 '(' 并且不要AS dateF像 Gordon linoff 建议的那样放置别名(它会导致循环引用)。

这是最终的 SQL 查询:

SELECT 
          dateT, PC.id_serie
    FROM (
          SELECT id_serie, MIN(dateF) FROM fixings GROUP BY id_serie
    ) AS PC1
    INNER JOIN
    (
        (
          SELECT target_calendar.dateT, series.id_serie FROM target_calendar, series
         ) AS PC LEFT JOIN fixings ON (PC.DateT=fixings.dateF) AND (PC.id_serie = fixings.id_serie) 
   )
 ON
  (
         PC1.dateF = PC.dateT
  )
    WHERE 
         fixings.dateF Is Null
    AND 
          DateT <= Date();
于 2013-08-01T13:23:10.723 回答
0

至少一个问题是您没有定义PC1.DateF. 您fixings.DateFwhere子句中也有,但fixings在该范围内没有调用表别名。您需要在第一个子查询中的列别名:

SELECT 
      dateT, PC.id_serie
FROM (
      SELECT id_serie, MIN(dateF) as dateF FROM fixings GROUP BY id_serie
) AS PC1
INNER JOIN
(
      SELECT target_calendar.dateT, series.id_serie FROM target_calendar, series  LEFT JOIN fixings ON (PC.DateT=fixings.dateF) AND (PC.id_serie = fixings.id_serie)
) AS PC
ON
      PC1.dateF = PC.dateT
AND 
      (((pc1.[dateF]) Is Null))
AND 
      DateT <= Date();

from您还在第二个子查询的子句中混合了旧式联接和新式联接。这些都应该是新式连接。

于 2013-08-01T12:13:04.587 回答