* [ ] 功能
* 根據兩個或多個表中的列之間的關系關聯,從這些表中查詢數據
* 支持多次連貫調用
* [ ] 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' => '二班',
),
);
~~~
- 開始使用
- 配置文件
- 路由模式
- AutoLoad類
- 啟動文件
- __construct
- SetRouting
- SetAlias
- SetStop
- SetError
- Access
- SetWorker
- SetClassFile
- SetClassDir
- Run
- OpenLoad
- LinuxStartAll
- Session類
- 使用說明
- set
- get
- delete
- pull
- has
- id
- Cookie類
- 使用說明
- set
- get
- delete
- pull
- has
- TempLets類
- 模板語法
- 模板標簽
- html
- show
- assign
- obtain
- Request類
- get
- post
- host
- referer
- getip
- localip
- header
- body
- file
- scheme
- protocolversion
- uri
- path
- querystring
- method
- Response
- SendFile
- FileStream
- SendData
- SetStatus
- SetHead
- SetMime
- WebSend
- redirect
- dumpJson
- dump
- come
- ps
- Frame類
- GetWeb
- ViewFile
- RoutingData
- SetClassFile
- SetClassDir
- GetMime
- FileMime
- LoadDir
- StartDir
- IsJson
- ArrJson
- JsonFormat
- ObStart
- GetConfig
- ConfigDir
- TempDir
- GetRunData
- GetStatic
- IsDebug
- SetDebug
- GetDebugInfo
- GlobalVariables類
- 使用說明
- set
- get
- delete
- pull
- has
- id
- Mysql類
- 新版本
- 第三方
- Thinkorm
- Medoo
- 舊版本
- Mysql 配置格式
- 項目中操作數據庫
- 項目場景
- 項目數據庫配置
- 項目數據庫中間類
- 項目中操作數據表
- 連貫操作
- where
- table
- data
- order
- field
- limit
- page
- group
- having
- join
- tabname
- union
- sql
- link
- link_base
- lock
- CURD 操作
- 寫入數據
- 數據刪除
- 數據查詢
- 數據更新
- 數據統計操作
- count
- sum
- max
- min
- avg
- 操作DEMO
- CurdTrait.php
- 項目Model層操作表.md
- Curl類
- Method類
- SslAes類
- layui_zqadmin