1

我正在努力结合以下 Select Statments,我知道我可以作弊并在其中添加一些假列,然后使用 Union,但我想正确地做到这一点。一旦我让他们加入,我将把 Statment 放入 XML 文件中,以便与 Word 和 CRM4 一起使用。

SELECT BILLTO_NAME, 
       BILLTO_LINE1, 
       BILLTO_LINE2, 
       BILLTO_LINE3, 
       BILLTO_CITY, 
       BILLTO_COUNTRY, 
       BILLTO_POSTALCODE, 
       ORDERNUMBER, 
       REQUESTDELIVERYBY, 
       MODIFIEDON, 
       SHIPTO_NAME, 
       SHIPTO_LINE1, 
       SHIPTO_LINE2, 
       SHIPTO_LINE3, 
       SHIPTO_CITY, 
       SHIPTO_STATEORPROVINCE, 
       SHIPTO_COUNTRY, 
       SHIPTO_POSTALCODE, 
       CREATEDBY 
FROM   SALESORDERBASE 



SELECT QUANTITY, 
       DESCRIPTION 
FROM   SALESORDERDETAILBASE 

SELECT NEW_ORDERNOTES, 
       NEW_NOTES 
FROM   SALESORDEREXTENSIONBASE 

它们都有 SalesOrderID 的公共列,我也需要在某处添加它。

4

2 回答 2

1

您可以LEFT JOIN在桌子上使用 a:

SELECT ob.SalesOrderID 
       ob.BILLTO_NAME, 
       ob.BILLTO_LINE1, 
       ob.BILLTO_LINE2, 
       ob.BILLTO_LINE3, 
       ob.BILLTO_CITY, 
       ob.BILLTO_COUNTRY, 
       ob.BILLTO_POSTALCODE, 
       ob.ORDERNUMBER, 
       ob.REQUESTDELIVERYBY, 
       ob.MODIFIEDON, 
       ob.SHIPTO_NAME, 
       ob.SHIPTO_LINE1, 
       ob.SHIPTO_LINE2, 
       ob.SHIPTO_LINE3, 
       ob.SHIPTO_CITY, 
       ob.SHIPTO_STATEORPROVINCE, 
       ob.SHIPTO_COUNTRY, 
       ob.SHIPTO_POSTALCODE, 
       ob.CREATEDBY,
       od.QUANTITY,
       od.DESCRIPTION,
       oe.NEW_ORDERNOTES,
       oe.NEW_NOTES
FROM   SALESORDERBASE ob
LEFT JOIN SALESORDERDETAILBASE od
    on ob.SalesOrderID = od.SalesOrderID
LEFT JOIN SALESORDEREXTENSIONBASE oe
    on ob.SalesOrderID = oe.SalesOrderID
于 2012-10-15T18:47:51.153 回答
0

假设标识关系的列在所有三个表上都称为 id,您可以这样做:

 SELECT sob.BILLTO_NAME, 
   sob.BILLTO_LINE1, 
   sob.BILLTO_LINE2, 
   sob.BILLTO_LINE3, 
   sob.BILLTO_CITY, 
   sob.BILLTO_COUNTRY, 
   sob.BILLTO_POSTALCODE, 
   sob.ORDERNUMBER, 
   sob.REQUESTDELIVERYBY, 
   sob.MODIFIEDON, 
   sob.SHIPTO_NAME, 
   sob.SHIPTO_LINE1, 
   sob.SHIPTO_LINE2, 
   sob.SHIPTO_LINE3, 
   sob.SHIPTO_CITY, 
   sob.SHIPTO_STATEORPROVINCE, 
   sob.SHIPTO_COUNTRY, 
   sob.SHIPTO_POSTALCODE, 
   sob.CREATEDBY, 
   sodb.QUANTITY, 
   sodb.DESCRIPTION,
   soeb.NEW_ORDERNOTES, 
   soeb.NEW_NOTES 
 From SalesOrderBase sob
 JOIN SalesOrderDetailBase sodb
   ON sob.id = sodb.SalesOrderID
 JOIN SalesOrderExtensionBase soeb
   ON sob.id = soeb.SalesOrderID

您可以将 JOINing 视为将行并排拼在一起,而 UNIONing 是将行一个接一个地拼在一起。UNION 要求列相同,而 JOIN 要求每行之间存在某种关系。

编辑 - OP提供了更多细节

于 2012-10-15T18:44:52.923 回答