-1

以下是我的数据库的架构。我一直在进行查询,但无法获得正确的结果。

TABLE: CUSTOMERINFORMATION  
FIELDS: CUSTID, ACTOPTIONID,VAT,MINIMUMDOCT 

TABLE: ONLINEORDERS 
FIELDS: CUSTID, TOTALQUANTITY 

TABLE: OFFLINEORDERS
FIELDS: CUSTID, TOTALQUANTITY 

TABLE: ACTOPTS 
FIELDS: ACTOPTIONID,ACTNAME 

像这样的输出:临时表:CUSTID、ACTOPTIONID、TOTALONLINEQ、TOTALOFFLINEQ、ACTNAME

我需要将上述所有表格和提到的字段合并到一个表格中。我们还需要确保返回 NULL 行;表示以上所有内容的 UNION ALL。

这是为了创建一个数据导出实用程序,将上述所有内容导出到另一个表中。但我们需要确保即使表中没有与其他记录匹配的记录,也不会排除任何记录。

已编辑

以下是我的查询:

SELECT table1.*, table2.*, table3.*
  FROM (SELECT ao.actname,
               ci.custid,
               ci.actoptionid,
               ci.minimumdoct,
               ci.vat
          FROM customerinformation ci, actoptions ao
         WHERE ci.actoptionid = ao.actoptionid) table1
       LEFT JOIN (SELECT custid, totalquantity, FROM onlineorders) table2
          ON table1.custid = table2.custid
       LEFT JOIN (SELECT custid FROM offlineorders) table3
          ON table2.custid = table3.custid

但是,上述内容不会从 RIGHT 上的表中返回行。我想从右边的表中返回 ROWS,为此我认为我需要在所有这些上使用 UNION ALL。我怎么做?

4

4 回答 4

1
SELECT
*
FROM
`CUSTOMERINFORMATION`
LEFT OUTER JOIN
`ONLINEORDERS` ON (`CUSTID`)
LEFT OUTER JOIN
`OFFLINEORDERS` ON (`CUSTID`)
LEFT OUTER JOIN
`OFFLINEORDERS` ON (`ACTOPTS`)
于 2012-12-25T15:33:09.017 回答
0

你想要这个吗?

    SELECT CI.CUSTID,
          CI.VAT,
          CI.MINIMUMDOCT,
          ONL.TOTALQUANTITY as ONLINE_TOTALQUANTITY,
          OFF.TOTALQUANTITY as OFFLINE_TOTALQUANTITY,
          A.ACTNAME 
   FROM CUSTOMERINFORMATION CI
        LEFT JOIN ACTOPTS A
          ON CI.ACTOPTIONID = A.ACTOPTIONID
        LEFT JOIN ONLINEORDERS ONL
          ON CI.CUSTID = ONL.CUSTID
        LEFT JOIN OFFLINEORDERS OFF
          ON CI.CUSTID = OFF.CUSTID
于 2012-12-25T15:26:41.463 回答
0

试试这个:

SELECT CI.CUSTID, CI.ACTOPTIONID, O.TOTALQUANTITY TOTALONLINEQ, 
       O1.TOTALQUANTITY TOTALOFFLINEQ, A.ACTNAME, 
       (O.TOTALQUANTITY + O1.TOTALQUANTITY) TotalQuantity  
FROM CUSTOMERINFORMATION CI 
INNER JOIN ACTOPTS A ON CI.ACTOPTIONID = A.ACTOPTIONID
LEFT JOIN (SELECT * FROM (SELECT CUSTID, TOTALQUANTITY FROM ONLINEORDERS ORDER BY DATE DESC) ASs A GROUP BY CUSTID) O ON CI.CUSTID = O.CUSTID 
LEFT JOIN (SELECT * FROM (SELECT CUSTID, TOTALQUANTITY FROM OFFLINEORDERS ORDER BY DATE DESC) ASs A GROUP BY CUSTID) O1 ON CI.CUSTID = O1.CUSTID;
于 2012-12-25T15:36:58.143 回答
0

如果您想在OR表FULL JOIN上匹配,请尝试使用LEFTRIGHT

附带说明一下,该UNION命令连接了两个SELECT语句,您在当前形式中没有这些语句。如果要使用UNION,则应将语句重新排序为SELECT ... UNION SELECT ...

于 2012-12-25T16:22:50.020 回答