mysql:无法加入子查询

kmbjn2e3  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(197)

如果有任何反馈,我将不胜感激。我的代码在下面,错误代码是1054“on”条款中的“iata”子条款未知。

SELECT     SUM(CASE 
           WHEN ABS(TZ_NUM_DEP-TZ_NUM_ARR)>=2 THEN 1 ELSE 0
           END AS Total)
FROM ontime AS o1
INNER JOIN (SELECT SUBSTRING_INDEX(a1.Name,' Airport',1) AS Departure,
       CASE 
       WHEN a1.Timezone='America/New_York (Eastern)' THEN 1
       WHEN a1.TimeZone='America/Chicago (Central)' THEN 2
       WHEN a1.Timezone='America/Denver and America/Phoenix (Mountain)' THEN 3
       WHEN a1.Timezone='America/Los_Angeles (Pacific)' THEN 4
       END AS TZ_NUM_Dep,
       SUBSTRING_INDEX(a2.Name,' Airport',1) AS Arrival,
       CASE 
       WHEN a2.Timezone='America/New_York (Eastern)' THEN 1
       WHEN a2.TimeZone='America/Chicago (Central)' THEN 2
       WHEN a2.Timezone='America/Denver and America/Phoenix (Mountain)' THEN 3
       WHEN a2.Timezone='America/Los_Angeles (Pacific)' THEN 4
       ELSE NULL
       END AS TZ_NUM_Arr
FROM ontime AS o2
LEFT JOIN airports AS a1
ON o2.Origin=a1.IATA
LEFT JOIN airports AS a2
ON o2.Dest=a2.IATA
WHERE a1.Country='United States' AND a2.Country='United States' AND Cancelled=0 AND Diverted=0) subq1
ON o1.Origin=subq1.IATA;
jdgnovmf

jdgnovmf1#

子查询中没有名为subq1的列iata。。尝试在子查询中添加列,例如:
而你却完全处于错误的位置

SELECT     SUM(
            CASE 
             WHEN ABS(TZ_NUM_DEP-TZ_NUM_ARR)>=2 THEN 1 ELSE 0
             END ) AS Total 
  FROM ontime AS o1
  INNER JOIN (
          SELECT a1.IATA, 
          SUBSTRING_INDEX(a1.Name,' Airport',1) AS Departure,
         CASE 
         WHEN a1.Timezone='America/New_York (Eastern)' THEN 1
         WHEN a1.TimeZone='America/Chicago (Central)' THEN 2
         WHEN a1.Timezone='America/Denver and America/Phoenix (Mountain)' THEN 3
         WHEN a1.Timezone='America/Los_Angeles (Pacific)' THEN 4
         END AS TZ_NUM_Dep,
         SUBSTRING_INDEX(a2.Name,' Airport',1) AS Arrival,
         CASE 
         WHEN a2.Timezone='America/New_York (Eastern)' THEN 1
         WHEN a2.TimeZone='America/Chicago (Central)' THEN 2
         WHEN a2.Timezone='America/Denver and America/Phoenix (Mountain)' THEN 3
         WHEN a2.Timezone='America/Los_Angeles (Pacific)' THEN 4
         ELSE NULL
         END AS TZ_NUM_Arr
  FROM ontime AS o2
  LEFT JOIN airports AS a1
  ON o2.Origin=a1.IATA
  LEFT JOIN airports AS a2
  ON o2.Dest=a2.IATA
  WHERE a1.Country='United States' AND a2.Country='United States' AND Cancelled=0 AND Diverted=0) subq1
  ON o1.Origin=subq1.IATA;
vlf7wbxs

vlf7wbxs2#

在子查询中,从不选择 IATA ,这意味着它不在“表”中 subq1 . 将其添加到子查询:

SELECT     SUM(CASE 
           WHEN ABS(TZ_NUM_DEP-TZ_NUM_ARR)>=2 THEN 1 ELSE 0
           END) AS Total
FROM ontime AS o1
INNER JOIN (SELECT SUBSTRING_INDEX(a1.Name,' Airport',1) AS Departure,
       a1.IATA AS IATA, -- Add this line
       CASE 
       WHEN a1.Timezone='America/New_York (Eastern)' THEN 1
       WHEN a1.TimeZone='America/Chicago (Central)' THEN 2
       WHEN a1.Timezone='America/Denver and America/Phoenix (Mountain)' THEN 3
       WHEN a1.Timezone='America/Los_Angeles (Pacific)' THEN 4
       END AS TZ_NUM_Dep,
       SUBSTRING_INDEX(a2.Name,' Airport',1) AS Arrival,
       CASE 
       WHEN a2.Timezone='America/New_York (Eastern)' THEN 1
       WHEN a2.TimeZone='America/Chicago (Central)' THEN 2
       WHEN a2.Timezone='America/Denver and America/Phoenix (Mountain)' THEN 3
       WHEN a2.Timezone='America/Los_Angeles (Pacific)' THEN 4
       ELSE NULL
       END AS TZ_NUM_Arr
FROM ontime AS o2
LEFT JOIN airports AS a1
ON o2.Origin=a1.IATA
LEFT JOIN airports AS a2
ON o2.Dest=a2.IATA
WHERE a1.Country='United States' AND a2.Country='United States' AND Cancelled=0 AND Diverted=0) subq1
ON o1.Origin=subq1.IATA;

相关问题