【问题标题】:LEFT JOIN in Azure CosmosDB SQL APIAzure CosmosDB SQL API 中的左联接
【发布时间】:2019-09-19 10:59:18
【问题描述】:

我使用 SQL API 在 Azure Cosmos DB 集合中使用 JOIN 来查询文档。

我有两份联系文件,一份有房产地址,另一份没有地址。

我需要获取所有人员的地址列表(包括没有任何地址的人员)。我已使用以下查询来执行此操作。 但它会给出有地址的人员列表。

有什么方法可以做 LEFT JOIN 吗?

查询:

从 ContactPerson 中选择 base.FirstName、base.LastName、Address.City base JOIN 地址 IN base.Address

示例文档:

[
  {
    "FirstName": "Saravana",
    "LastName": "Kumar",
    "Address": [
      {
        "City": "aaaa"
      },
      {
        "City": "bbbb"
      }
    ]
  },
  {
    "FirstName": "Jayanth",
    "LastName": "T"
  }
]

预期输出:

[
  {
    "FirstName": "Saravana",
    "LastName": "Kumar",
    "City": "aaa"
  },
  {
    "FirstName": "Saravana",
    "LastName": "Kumar",
    "City": "bbbb"
  },
  {
    "FirstName": "Jayanth",
    "LastName": "T"
  }
]

实际输出:

[
  {
    "FirstName": "Saravana",
    "LastName": "Kumar",
    "City": "bbbb"
  },
  {
    "FirstName": "Saravana",
    "LastName": "Kumar",
    "City": "bbbb"
  }
]

【问题讨论】:

  • 您好,我的回答对您有帮助吗?

标签: azure join azure-cosmosdb azure-cosmosdb-sqlapi


【解决方案1】:

据我所知,目前 cosmos db 不支持左连接,您可以对此 thread 投赞成票。

作为解决方法,我建议您在存储过程中使用 2 个不同的 sql,然后将结果合并到其中。

1.SELECT base.FirstName, base.LastName FROM ContactPerson base where NOT IS_DEFINED(base.Address)

2.SELECT base.FirstName, base.LastName, Address.City FROM ContactPerson base JOIN Address IN base.Address

SP:

function sample() {
    var collection = getContext().getCollection();

    var array = [];
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        'SELECT base.FirstName, base.LastName, Address.City FROM ContactPerson base JOIN Address IN base.Address',
    function (err, feed, options) {
        if (err) throw err;
        if (!feed || !feed.length) {
            var response = getContext().getResponse();
            response.setBody('no docs found');
        }
        else {
                array.push(feed);
        }
    });

    var isAccepted1 = collection.queryDocuments(
        collection.getSelfLink(),
        'SELECT base.FirstName, base.LastName FROM ContactPerson base where NOT IS_DEFINED(base.Address)',
    function (err, feed1, options) {
        console.log(222)
        if (err) throw err;
        if (!feed1|| !feed1.length) {
            var response = getContext().getResponse();
            response.setBody('no docs found');
        }
        else {

                array.push(feed1);

        }
    });
    var response = getContext().getResponse();
    response.setBody(array);

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}

您可以根据需要调整输出格式。

【讨论】:

    【解决方案2】:

    然后不要加入。您可以直接访问地址字段。例如:

    SELECT base.FirstName, base.LastName, Address.City FROM ContactPerson base
    

    如果没有属性,它只会显示 null 或空字符串

    【讨论】:

      猜你喜欢
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-13
      • 2019-07-22
      • 2021-01-25
      • 2018-06-30
      相关资源
      最近更新 更多