多个内部联接CAML查询

时间:2015-10-05 11:13:49

标签: sql sharepoint caml

我正在尝试将以下SQL语句转换为CAML查询:

SELECT        t.Id, t.Name, t.CustomerId
FROM            Ticket AS t 
              INNER JOIN
                         Customer AS c1 ON t.CustomerEMail = c1.EMail
              INNER JOIN
                         Customer AS c2 ON c1.CompanyNo = c2.CompanyNo
WHERE        (c2.Email = 'client1@co1.com')

使用CAMLJS,我得到了这个:( CompanyNo相当于Nav_CustomerNo)

var query = new CamlBuilder()
.View(["Title", ...])
.InnerJoin("ClientLookup", "c1")
.Select("EMail", "c1Email")
.InnerJoin("ClientLookup", "c2")
.Select("Nav_CustomerNo", "c2CompanyNo")
.Query()
.Where()
.All()
.ToString()

但我不确定如何从这里开始。如何转换

Customer AS c1 ON t.CustomerEMail = c1.EMail

行?我想的是: 。凡( “c1Email”)。EqualTo(??

这是等效的CAML:

<View>
    <ViewFields>
        <FieldRef Name="Title" />
        <FieldRef Name="Ticket_MainBody" />
        <FieldRef Name="Ticket_SupportID" />
        <FieldRef Name="ClientLookup" />
        <FieldRef Name="IsPrivateTicket" />
        <FieldRef Name="Ticket_IsFAQ" />
    </ViewFields>
    <Joins>
        <Join Type="INNER" ListAlias="c1">
            <Eq>
                <FieldRef Name="ClientLookup" RefType="ID" />
                <FieldRef Name="ID" List="c1" />
            </Eq>
        </Join>
        <Join Type="INNER" ListAlias="c2">
            <Eq>
                <FieldRef Name="ClientLookup" RefType="ID" />
                <FieldRef Name="ID" List="c2" />
            </Eq>
        </Join>
    </Joins>
    <ProjectedFields>
        <Field ShowField="EMail" Type="Lookup" Name="c1Email" List="c1" />
        <Field ShowField="Nav_CustomerNo" Type="Lookup" Name="c2CompanyNo" List="c2" />
    </ProjectedFields>
    <Query>
        <Where />
    </Query>
</View>

1 个答案:

答案 0 :(得分:0)

今天早上花了相当长的时间试图解决这个问题,我想我有一个解决方案。

首先 - 让我们从一些通用伪结构开始:

ParentTable:Id,Title,ChildTableReference

ChildTable:Id,Title,BabyTableReference

BabyTable:Id,Title

因此,我们有一个包含引用的父表。绑定到该引用的表包含对该表下的另一个表的引用。

这是一个简单的实例:

(父)CustomerOrder:Id,Title,CustomerID

(孩子)客户:Id,Title,RegionID

(宝贝)地区:Id,标题

那么,如果我们想要以下查询响应怎么办?

CustomerOrder_Id,CustomerOrder_Title,Customer_Title,Region_Title

XML的第一部分很简单:

    <ViewFields>
        <FieldRef Name='ID' />
        <FieldRef Name='Title' />
        <FieldRef Name='Customer_Title' />
        <FieldRef Name='Region_Title' />
    </ViewFields>        

但是我们如何将桌子拼接在一起?我们使用连接。

第一次加入非常简单:

    <Join Type='INNER' ListAlias='Customers'>
        <Eq>
            <FieldRef Name='CustomerID' RefType='ID' />
            <FieldRef List='Customer' Name='ID' />
        </Eq>
    </Join>

第一次加入是好的,如果我们想要做的就是查看客户名称,我们只需要添加我们的投影,我们就可以了。但是,我们也想要这个客户的区域。为此,我们需要第二次加入。第二次连接的技巧是向FIRST FieldRef语句添加列表引用。确切的行是: FieldRef List ='Customers'Name ='RegionID'RefType ='ID'。请注意,List =属性指向Child引用的别名。换句话说,有一个名为Customers的引用列表,其中包含一个名为RegionID的列,它是ID的reftype。这需要等于我们基于ID属性上的根表Region的名为Regions的新别名表。

  <Join Type='INNER' ListAlias='Regions'>
      <Eq>
          <FieldRef List='Customers' Name='RegionID' RefType='ID' />
          <FieldRef List='Regions' Name='ID' />
      </Eq>
  </Join>

我们现在几乎回家了。此时我们需要创建Projected Fields来吐出Region.Title和Customer.Title属性。

  <ProjectedFields>
        <Field ShowField='Title' Type='Lookup' Name='Customer_Title' List='Customers' />
        <Field ShowField='Title' Type='Lookup' Name='Region_Title' List='Regions' />

    </ProjectedFields>

然后将您的所有内容放在一起,您的CAML查询将是:

//Completed query
<View>
   <ViewFields>
        <FieldRef Name='ID' />
        <FieldRef Name='Title' />
        <FieldRef Name='Customer_Title' />
        <FieldRef Name='Region_Title' />
    </ViewFields>
    <Joins>
        <Join Type='INNER' ListAlias='Customers'>
            <Eq>
                <FieldRef Name='CustomerID' RefType='ID' />
                <FieldRef List='Customer' Name='ID' />
            </Eq>
         </Join>
         <Join Type='INNER' ListAlias='Regions'>
            <Eq>
                <FieldRef List='Customers' Name='RegionID' RefType='ID' />
                <FieldRef List='Regions' Name='ID' />
            </Eq>
         </Join>
    </Joins>
    <ProjectedFields>
        <Field ShowField='Title' Type='Lookup' Name='Customer_Title' List='Customers' />
        <Field ShowField='Title' Type='Lookup' Name='Region_Title' List='Regions' />
    </ProjectedFields>
    <Query />
</View>

那应该让你一路走来。此外 - 如果支持字段类型(text,refid,number等),您可以在已加入的任何元素上查询(例如Where子句)。请记住,如果您要查询说明区域的ID,您需要将ID添加到ProjectedFields。

// completed query with WHERE clause filtering by BABY table ID.
<View>
   <ViewFields>
        <FieldRef Name='ID' />
        <FieldRef Name='Title' />
        <FieldRef Name='Customer_Title' />
        <FieldRef Name='Region_Title' />
    </ViewFields>
    <Joins>
        <Join Type='INNER' ListAlias='Customers'>
            <Eq>
                <FieldRef Name='CustomerID' RefType='ID' />
                <FieldRef List='Customer' Name='ID' />
            </Eq>
         </Join>
         <Join Type='INNER' ListAlias='Regions'>
            <Eq>
                <FieldRef List='Customers' Name='RegionID' RefType='ID' />
                <FieldRef List='Regions' Name='ID' />
            </Eq>
         </Join>
    </Joins>
    <ProjectedFields>
        <Field ShowField='Title' Type='Lookup' Name='Customer_Title' List='Customers' />
    <Field ShowField='ID' Type='Lookup' Name='Region_ID' List='Regions' />
        <Field ShowField='Title' Type='Lookup' Name='Region_Title' List='Regions' />
    </ProjectedFields>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name='Region_ID' LookupId='True' />
                <Value Type='Integer'>1</Value>
            </Eq>
        </Where>
    </Query>
</View>