如何在INFORMIX SQL中LEFT JOIN

时间:2017-09-13 09:05:50

标签: sql informix

我有一个针对Informix数据库的以下工作sql查询。

SELECT soi_seq,soi_cusitem,max(soi_product) as ProductCode, 
       max(soi_custprod) as CustProductCode, max(soi_desc) AS ProductDesc, 
       max(soi_comdt2) AS RequiredDate, sum(soi_itqty) as Qty, 
       sum(soi_invval) as Nett, max(soi_discper) as Discount, max(soi_per) 
       as Per, max(soi_price) as ItemPrice 
       from soitem 
       where soi_ordref= ? group by soi_seq,soi_cusitem, soi_lineref 
       order by soi_cusitem asc

我需要加入其他两个表,但不知道如何使用informix语法执行此操作, 基本上我需要在上面的连接上面添加查询:

       Left Join cmprod on soi_product = cmp_product
       Left Join cmass on cmp_product = cmass_product 

并且还将以下内容添加到WHERE子句中:

      AND cmass_assem Is Null

我可以使用与Informix数据库的旧MS Access连接查询并获取所需结果,但不能在代码中查询。

2 个答案:

答案 0 :(得分:0)

这应该有效

SELECT soi_seq, soi_cusitem, soi_lineref,
       MAX(soi_product) AS ProductCode, 
       MAX(soi_custprod) AS CustProductCode,
       MAX(soi_desc) AS ProductDesc, 
       MAX(soi_comdt2) AS RequiredDate,
       SUM(soi_itqty) AS Qty, 
       SUM(soi_invval) AS Nett,
       MAX(soi_discper) AS Discount,
       MAX(soi_per) AS Per,
       MAX(soi_price) AS ItemPrice 
FROM soitem
LEFT JOIN cmprod ON soi_product = cmp_product
LEFT JOIN cmass ON cmp_product = cmass_product 
WHERE soi_ordref = ?
      AND cmass_assem IS NULL
GROUP BY 1,2,3 
ORDER BY 2

答案 1 :(得分:0)

以前的Informix版本使用 outer 关键字,如下所示:

SELECT soi_seq, soi_cusitem, soi_lineref,
       MAX(soi_product) AS ProductCode, 
       MAX(soi_custprod) AS CustProductCode,
       MAX(soi_desc) AS ProductDesc, 
       MAX(soi_comdt2) AS RequiredDate,
       SUM(soi_itqty) AS Qty, 
       SUM(soi_invval) AS Nett,
       MAX(soi_discper) AS Discount,
       MAX(soi_per) AS Per,
       MAX(soi_price) AS ItemPrice 
FROM soitem, -- look, it uses comma to separate tables, even if outer is used
   outer (cmprod CMP, 
   outer cmass CMA)
   -- it requires to order tables with parenthesis
   -- I assume soitem->cmprod->cmass
WHERE 
   -- for good understanding, resolve joins first
   soi_product = CMP.cmp_products 
   AND CMP.cmp_product = CMA.cmass_product 
   -- then match your selection
AND oi_ordref = ? 

很抱歉,答案迟到了一年,我将其留在这里供将来的搜索者使用

Informix outer joins