查询中带有'union'和'order by'的错误'from-clause entry'

时间:2014-06-08 14:28:31

标签: postgresql

我有一个包含3个联合和一个order by子句的查询。

因此,如果s1,s2,s3和s4是包含select查询的字符串变量,而oc是包含order by子句的字符串变量,则完整查询为:

s1 & " union " & s2 & " union " & s3 & " union " & s4 & oc

每个select都具有完全相同的格式,只是不同的参数,每个都包含许多连接。

此查询失败并显示错误

  

运行时错误:' -2147467259(80004005)':

     

缺少from-clause条目表#34; eventTbl&#34 ;;错误的同时   执行查询

但是,以下所有变体都不会导致错误

s1 & " union " & s2 & " union " & s3 & " union " & s4

s1 & oc

s2 & oc

s3 & oc

s4 & oc

导致错误的查询在.mdb格式的同一数据库上正常工作。

postgresql服务器是postgres 9.0 程序是vb6,ADO 2.6,连接字符串是

  

提供商= MSDASQL;驱动器= {的PostgreSQL   ANSI}; SERVER =本地主机; DATABASE = dbname;开发UID = postgres的; PWD =密码; CONNSETTINGS = SET   Datestyle TO   ' DMY'%3B; BOOLSASCHAR = 0; TEXTASLONGVARCHAR = 1; TrueIsMinus1 = 1;

EDIT 每个选择的结构如下:

 select Charges.CHARGESCALENAMEF1, MATERIALST12.MATERIALNAMEF1
   , workspecificationst11.*
   , clientdetail.clientidf9
   , eventTbl.*
 from Charges
 right join (MATERIALST12 
            right join  (workspecificationst11 
                         right join (clientdetail
                                      right join  eventTbl
                                      on clientdetail.clientidf9 = eventTbl.clientidf2
                                    ) on workspecificationst11.serialnumf5 = eventTbl.workf5
                         ) ON MATERIALST12.SERIALNUMF3 = eventTbl.MATERIALF8
           ) ON Charges.SERIALNUMF2 = eventTbl.CHARGESCALEF7
 where eventTbl.clientidf2 = 15249
 and  eventTbl.workf5 = 40
 and  eventTbl.workstatusf14 = 2
 and eventTbl.section = 1
 and workspecificationst11.showinaccountsf9 = true
  ;

3 个答案:

答案 0 :(得分:3)

删除所有括号:

select Charges.CHARGESCALENAMEF1, MATERIALST12.MATERIALNAMEF1
   , workspecificationst11.*
   , clientdetail.clientidf9
   , eventTbl.*
 from Charges
 right join eventTbl ON Charges.SERIALNUMF2 = eventTbl.CHARGESCALEF7
 right join clientdetail on clientdetail.clientidf9 = eventTbl.clientidf2
 right join MATERIALST12 ON MATERIALST12.SERIALNUMF3 = eventTbl.MATERIALF8
 right join workspecificationst11 on workspecificationst11.serialnumf5 = eventTbl.workf5
 where eventTbl.clientidf2 = 15249
 and eventTbl.workf5 = 40
 and eventTbl.workstatusf14 = 2
 and eventTbl.section = 1
 and workspecificationst11.showinaccountsf9 = true

答案 1 :(得分:2)

如果我理解正确,完整的查询最终会看起来像下面这样,虽然我已经将它简化为两个SELECT语句,它们之间有UNION,最后一个ORDER BY条款。根据您的上述评论,您发现:

table alias子句中的eventTbl(在您的情况下为ORDER BY),当查询包含UNION时不起作用

我怀疑这是因为每个select查询都有eventTbl的本地表别名,但总的来说查询在整个查询的范围内没有表别名。此外,ORDER BY适用于整个结果集,而不适用于单个SELECT语句。

SELECT Charges.CHARGESCALENAMEF1, MATERIALST12.MATERIALNAMEF1
   , workspecificationst11.*
   , clientdetail.clientidf9
   , eventTbl.*
 from Charges
 right join (MATERIALST12 
            right join  (workspecificationst11 
                         right join (clientdetail
                                      right join  eventTbl
                                      on clientdetail.clientidf9 = eventTbl.clientidf2
                                    ) on workspecificationst11.serialnumf5 = eventTbl.workf5
                         ) ON MATERIALST12.SERIALNUMF3 = eventTbl.MATERIALF8
           ) ON Charges.SERIALNUMF2 = eventTbl.CHARGESCALEF7
 where eventTbl.clientidf2 = 15249
 and  eventTbl.workf5 = 40
 and  eventTbl.workstatusf14 = 2
 and eventTbl.section = 1
 and workspecificationst11.showinaccountsf9 = true

UNION

SELECT Charges.CHARGESCALENAMEF1, MATERIALST12.MATERIALNAMEF1
   , workspecificationst11.*
   , clientdetail.clientidf9
   , eventTbl.*
 from Charges
 right join (MATERIALST12 
            right join  (workspecificationst11 
                         right join (clientdetail
                                      right join  eventTbl
                                      on clientdetail.clientidf9 = eventTbl.clientidf2
                                    ) on workspecificationst11.serialnumf5 = eventTbl.workf5
                         ) ON MATERIALST12.SERIALNUMF3 = eventTbl.MATERIALF8
           ) ON Charges.SERIALNUMF2 = eventTbl.CHARGESCALEF7
 where eventTbl.clientidf2 = 15249
 and  eventTbl.workf5 = 40
 and  eventTbl.workstatusf14 = 2
 and eventTbl.section = 1
 and workspecificationst11.showinaccountsf9 = true
ORDER BY eventbl.datef1, eventtbl.eventidf15;

ORDER BY中移除表别名,因为作为单个查询,上面的内容会返回datef1列和eventiff15列,但不会定义整个结果集中的表别名。

<强>解
ORDER BY子句中删除表别名:

ORDER BY datef1, eventidf15;

如果你做了类似的事情,我想你可以让表别名起作用:

SELECT * FROM
( Original full query goes here without the order by) AS eventTbl
ORDER BY eventbl.datef1, eventtbl.eventidf15;

但这有点过分。

答案 2 :(得分:1)

建议重构:

WITH parms as (
   SELECT 15249 as clntid, 40 as wrkf, 2 as wkstat, sect = 1
   UNION ALL
   SELECT 14349 as clntid, 37 as wrkf, 23 as wkstat, sect = 5
   UNION ALL
   SELECT 16822 as clntid, 106 as wrkf, 55 as wkstat, sect = 2
   UNION ALL
   SELECT 24253 as clntid, 9 as wrkf, 43 as wkstat, sect = 9
)
from 
    CHARGES
       right join 
    eventTbl 
          ON Charges.SERIALNUMF2 = eventTbl.CHARGESCALEF7
       right join 
    clientdetail 
          on clientdetail.clientidf9 = eventTbl.clientidf2
       right join 
    MATERIALST12 
          ON MATERIALST12.SERIALNUMF3 = eventTbl.MATERIALF8
       right join 
    workspecificationst11 
          on workspecificationst11.serialnumf5 = eventTbl.workf5
       RIGHT JOIN 
    PARMS 
          ON (
              eventTbl.clientidf2 = PARMS.clntid
              and eventTbl.workf5 = PARMS.wrkf
              and eventTbl.workstatusf14 = PARMS.wkstat
              and eventTbl.section = PARMS.sect
          )
WHERE
   workspecificationst11.showinaccountsf9 = true