错误SQL状态:42P01在postgres中

时间:2017-03-20 14:10:34

标签: sql postgresql psql

我有这个大选择我认为是正确的,我必须执行,但我不知道它为什么会给我一个错误。我的所有桌子都是单桌,在它们之间我没有任何关系。什么发生了什么?当它发生在第二个ON时:

select DISTINCT t0_0.id_pord, t0_0.prod_fam, t0_0.prod_nam, t0_0.prod_mod  
from inventory t0_0 
INNER JOIN ( select DISTINCT c0.id_dev 
             FROM ps_det c0 
             INNER JOIN ( select DISTINCT c0.id_dev 
                          FROM ps_det c0 
                          INNER JOIN mtx_det c1 
                            ON c0.psirtid = c1.alert_id  ) t4_0 
               ON t0_0.id_dev = t3_0.id_dev
             WHERE c0.invent = '1333' 
             AND c0.cust = '598' ) t3_0 
  ON t0_0.id_dev = t3_0.id_dev  
where  t0_0.prod_fam IN ('Cisco', 'Catalyst', '3750 Series') 
AND t0_0.invent ='1333'
AND t0_0.cust = '598'

它给了我这个错误:

ERROR:  invalid reference to FROM-clause entry for table "t0_0"
LINE 1: mtx_det c1 ON c0.psirtid = c1.alert_id) t4_0 ON t0_0.id_dev...
                                                             ^
HINT:  There is an entry for table "t0_0", but it cannot be referenced from this part of the query.
********** Error **********

ERROR: invalid reference to FROM-clause entry for table "t0_0"
SQL state: 42P01
Hint: There is an entry for table "t0_0", but it cannot be referenced from this part of the query.
Character: 352

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您已从其中一个子查询中引用了t0_0:

select DISTINCT c0.id_dev 
FROM ps_det c0 
INNER JOIN ( select DISTINCT c0.id_dev 
             FROM ps_det c0 
             INNER JOIN mtx_det c1 
               ON c0.psirtid = c1.alert_id  ) t4_0 
  ON t0_0.id_dev = t3_0.id_dev -- HERE'S YOUR PROBLEM
WHERE c0.invent = '1333' 
             AND c0.cust = '598' 

每个子查询必须包含自己的别名,因此子查询应为:

select DISTINCT c0.id_dev 
FROM ps_det c0 
INNER JOIN ( select DISTINCT c0.id_dev 
             FROM ps_det c0 
             INNER JOIN mtx_det c1 
               ON c0.psirtid = c1.alert_id  ) t4_0 
  ON t4_0.id_dev = c0.id_dev -- HERE'S THE FIX
WHERE c0.invent = '1333' 
             AND c0.cust = '598'