• inner join
    • 语法
    • 参数
    • 返回值
    • 示例

    inner join

    用于根据两个或多个集合中的字段名之间的关系,从这些集合中查询数据。

    语法

    <collection1_name | (select_set1)> as <alias1_name> inner join <collection2_name | (select_set2)> as <alias2_name> [on condition]

    参数

    参数名参数类型描述是否必填
    collection1_name/collection2_namestring集合名。
    select_set1/select_set2set结果集。
    alias1_name/alias2_namestring别名。
    conditionexpression集合之间关联条件。

    返回值

    在集合中存在至少一个匹配时,inner join 返回匹配的记录。

    示例

    • 集合 foo.persons 中记录如下。
    1. { "Id_P": 1, "LastName": "Adams", "FirstName": "John", "Address": "Oxford Street", "City": "London" }
    2. { "Id_P": 2, "LastName": "Bush", "FirstName": "George", "Address": "Fifth Avenue", "City": "New York" }
    3. { "Id_P": 3, "LastName": "Carter", "FirstName": "Thomas", "Address": "Changan Street", "City": "Beijing" }
    • 集合 foo.orders 中记录如下。
    1. { "Id_O": 1, "OrderNo": 77895, "Id_P": 3 }
    2. { "Id_O": 2, "OrderNo": 44678, "Id_P": 3 }
    3. { "Id_O": 3, "OrderNo": 22456, "Id_P": 1 }
    4. { "Id_O": 4, "OrderNo": 24562, "Id_P": 1 }
    5. { "Id_O": 5, "OrderNo": 34764, "Id_P": 65 }
    • 列出所有人的订购。
    1. > db.exec("select t1.LastName, t1.FirstName, t2.OrderNo from foo.persons as t1 inner join foo.orders as t2 on t1.Id_P=t2.Id_P")
    2. { "LastName": "Adams", "FirstName": "John", "OrderNo": 22456 }
    3. { "LastName": "Adams", "FirstName": "John", "OrderNo": 24562 }
    4. { "LastName": "Carter", "FirstName": "Thomas", "OrderNo": 77895 }
    5. { "LastName": "Carter", "FirstName": "Thomas", "OrderNo": 44678 }
    6. Return 4 row(s).
    7. Takes 0.43722s.

    Note:

    • 不能包含非联合条件,如下写法是错误的:

      1. > db.exec( "select T1.a, T2.b from foo.bar1 as T1 inner join foo.bar2 as T2 on T1.a < 10" )
    • 不能在 inner join 本层使用 select * 语句。