企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
* [ ] 功能 * 根据两个或多个表中的列之间的关系关联,从这些表中查询数据 * 支持多次连贯调用 * [ ] join 方法参数 * 四个参数: * $data,关联的表数组 * $type,关联类型,可选值有: | 可选值 | 说明 | | :---: | :---: | | table | 连表,不需要关联条件,默认为 table | | left | 左连接,需要关联条件 | | right | 右连接,需要关联条件 | | inner | 内连接,等值连接,需要关联条件 | * $where,关联条件,默认为空数组 * $orand,关联条件的关系,默认为 and * 场景: user 数据库 user_class 表 | class_id | class_name | | :---: | :---: | | 1 | 一班 | | 2 | 二班 | | 3 | 三班 | user_account 表 | uid | username | class_id | | :---: | :---: | :---: | | 1 | 张三 | 1 | | 2 | 李四 | 2 | | 3 | 隔壁老王 | 1 | * 用法一:关联表不写别名,那么关联表就要写完整表名 ~~~ // 关联表 $data = array( 'user_account', 'user_class', ); // 关联类型 $type = 'left'; // 关联条件 $join_where = array( 'user_account.class_id[.=]' => 'user_class.class_id', ); // 关联条件的关系 $orand = 'and'; // 查询条件 $where = array( 'user_class.class_name' => '二班', ); // 关联查询 $data = mysql\User::join( $data, $type, $join_where, $orand )->where( $where )->select(); ~~~ 执行的 sql 语句: ~~~ select * from  user_account left join user_class on user_account.class_id = user_class.class_id where user_class.class_name = '二班' ~~~ 查询结果: ~~~ array( array( 'uid' => 2, 'username' => '李四', 'class_id' => 2, 'class_name' => '二班', ), ); ~~~ * 用法二:关联表写别名,表名不用带表前缀 ~~~ // 关联表 // 定义表别名 // user_account 定义别名为 a // user_class 定义别名为 c $data = array( 'a[true]' => 'user_account', 'c[true]' => 'user_class', ); // 关联类型 $type = 'left'; // 关联条件 $join_where = array( 'a.class_id[.=]' => 'c.class_id', ); // 关联条件的关系 $orand = 'and'; // 查询条件 $where = array( 'c.class_name' => '二班', ); // 关联查询 $data = mysql\User::join( $data, $type, $join_where, $orand )->where( $where )->select(); ~~~ 执行的 sql 语句: ~~~ select * from  user.user_account as a left join user.user_class as c on a.class_id = c.class_id where c.class_name = '二班'; ~~~ 查询结果: ~~~ array( array( 'uid' => 2, 'username' => '李四', 'class_id' => 2, 'class_name' => '二班', ), ); ~~~