24.JOIN 子句

N1QL提供了连接,允许通过结合两个或两个以上的源对象组装新对象。

例如,假设有两个桶

users_with_orders orders_with_users。桶users_with_orders包含用户配置文件和他们的订单id列表,参考测试桶数据查看。

users_withorders的 KEY的唯一标示其实user_id的值

orders_with_users的 KEY唯一标示其实是order_id的标示

所以下面的意思是使用"Elinor_33313792"关键词去匹配users_with_orders桶中的唯一标识.

然后再内连接orders_with_users,并且orders_with_users的唯一标识必须满足user数据里的订单号值.

Query:

SELECT usr.personal_details, orders 
    FROM users_with_orders usr 
        USE KEYS "Elinor_33313792" 
            JOIN orders_with_users orders 
                ON KEYS ARRAY s.order_id FOR s IN usr.shipped_order_history END

结果:

{
  "results": [
    {
      "orders": {
        "doc_type": "order",
        "order_details": {
          "order_datetime": "Wed Jun  6 18:53:39 2012",
          "order_id": "T103929516925"
        },
        "payment_details": {
          "payment_mode": "Debit Card",
          "total_charges": 308
        },
        "product_details": {
          "currency": "EUR",
          "list_price": 318,
          "pct_discount": 5,
          "product_id": "P3109994453",
          "sale_price": 303
        },
        "shipping_details": {
          "shipping_charges": 5,
          "shipping_status": "Delivered",
          "shipping_type": "Express"
        },
        "user_id": "Elinor_33313792"
      },
      "personal_details": {
        "age": 60,
        "display_name": "Elinor Ritchie",
        "email": "Elinor.Ritchie@snailmail.com",
        "first_name": "Elinor",
        "last_name": "Ritchie",
        "state": "Arizona"
      }
    },
    {
      "orders": {
        "doc_type": "order",
        "order_details": {
          "order_datetime": "Thu Aug 11 18:53:39 2011",
          "order_id": "T573145204032"
        },
        "payment_details": {
          "payment_mode": "NetBanking",
          "total_charges": 569
        },
        "product_details": {
          "currency": "GBP",
          "list_price": 666,
          "pct_discount": 15,
          "product_id": "P9315874155",
          "sale_price": 567
        },
        "shipping_details": {
          "shipping_charges": 2,
          "shipping_status": "Delivered",
          "shipping_type": "Regular"
        },
        "user_id": "Elinor_33313792"
      },
      "personal_details": {
        "age": 60,
        "display_name": "Elinor Ritchie",
        "email": "Elinor.Ritchie@snailmail.com",
        "first_name": "Elinor",
        "last_name": "Ritchie",
        "state": "Arizona"
      }
    }
  ]
}

Last updated

Was this helpful?