将模式与表区分开来?

时间:2014-05-21 20:34:01

标签: sql postgresql schema

在Postgresql中,查询收到的错误是说架构“purchase_order”不存在。

from ((select a.item_no
    from stocka a
        join order_item oi
            on (oi.item_no = a.item_no)
        join purchase_order po
            on (po.order_no = oi.order_no)
    where po.location = 'LocationA'
    ) UNION ALL
    (select b.item_no
    from stockb b
        join order_item oi
            on (oi.item_no = b.item_no)
        join purchase_order po
            on (po.order_no = oi.order_no)
    where po.location = 'LocationB'
    ))

Union用于from子句

出于某种原因,buy_order不是表,而是模式。

1 个答案:

答案 0 :(得分:1)

您描述的错误不是由于您发布的代码,如果对象存在,这应该有效。

我只添加了alias for the subquerysub(必填!),简化为USING(可选),删除了多余的括号并重新格式化:

SELECT *
FROM  (
   SELECT a.item_no
   FROM   stocka a
   JOIN   order_item     oi USING (item_no)
   JOIN   purchase_order po USING (order_no)
   WHERE  po.location = 'LocationA'

   UNION ALL
   SELECT b.item_no
   FROM   stockb b
   JOIN   order_item     oi USING (item_no)
   JOIN   purchase_order po USING (order_no)
   WHERE  po.location = 'LocationB'
   ) sub;

根据数据库布局(表定义?)和确切要求,可以进一步简化。