【问题标题】:N1QL update document with subqueryN1QL 用子查询更新文档
【发布时间】:2020-05-03 14:03:17
【问题描述】:

我正在尝试对具有“regionName”字段的“Country”文档进行非规范化,该字段对应于另一个“Region”文档的“name”。我的 N1QL 查询如下,但它不起作用

update test as t1 set regionName = (
 select raw name from test as t2 where `_class`="Region"
) where `_class`="Country" and t1.regionCode=t2.code RETURNING *;

有什么帮助吗?

【问题讨论】:

    标签: couchbase n1ql


    【解决方案1】:

    N1QL 没有更新连接。不能在 UPDATE WHERE 子句中使用 SET CLAUSE 子查询源。

    使用合并

    MERGE test AS m 
    USING test AS s
    ON s.`_class`="Region" AND m.`_class`="Country" AND m.regionCode=s.code
    WHEN MATCHED THEN m.regionName = s.name;
    

    https://blog.couchbase.com/ansi-join-enhancements-and-ansi-merge/

    CB 6.0

    MERGE test AS m 
    USING  (SELECT META(c).id, r.name
            FROM test AS r
            JOIN test AS c 
            ON r.`_class`="Region" AND c.`_class`="Country" 
               AND r.regionCode = c.code) AS s
    ON KEY s.id
    WHEN MATCHED THEN m.regionName = s.name;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-13
      • 1970-01-01
      • 2020-12-11
      • 2023-03-16
      相关资源
      最近更新 更多