# 多表查询
多表查询
~~~
<?php
namespace Action;
use HY\Action;
class Index extends Action {
public function Index(){
//实例User表为对象
$User = S("User");
// [>] == LEFT JOIN
// [<] == RIGH JOIN
// [<>] == FULL JOIN
// [><] == INNER JOIN
$User->select("post", array(
// Here is the table relativity argument that tells the relativity between the table you want to join.
// The row author_id from table post is equal the row user_id from table account
"[>]account" => array("author_id" => "user_id"),
// The row user_id from table post is equal the row user_id from table album.
// This is a shortcut to declare the relativity if the row name are the same in both table.
"[>]album" => "user_id",
// [post.user_id is equal photo.user_id and post.avatar_id is equal photo.avatar_id]
// Like above, there are two row or more are the same in both table.
"[>]photo" => array("user_id", "avatar_id"),
// If you want to join the same table with different value,
// you have to assign the table with alias.
"[>]account (replyer)" => array("replyer_id" => "user_id"),
// You can refer the previous joined table by adding the table name before the column.
"[>]account" => array("author_id" => "user_id"),
"[>]album" => array("account.user_id" => "user_id"),
// Multiple condition
"[>]account" => array(
"author_id" => "user_id",
"album.user_id" => "user_id"
)
), array(
"post.post_id",
"post.title",
"account.user_id",
"account.city",
"replyer.user_id",
"replyer.city"
), array(
"post.user_id" => 100,
"ORDER" => "post.post_id DESC",
"LIMIT" => 50
));
// SELECT
// `post`.`post_id`,
// `post`.`title`,
// `account`.`city`
// FROM `post`
// LEFT JOIN `account` ON `post`.`author_id` = `account`.`user_id`
// LEFT JOIN `album` USING (`user_id`)
// LEFT JOIN `photo` USING (`user_id`, `avatar_id`)
// WHERE
// `post`.`user_id` = 100
// ORDER BY `post`.`post_id` DESC
// LIMIT 50
}
}
~~~
举一个通俗易懂的例子
两个表
post 与 user
post表的数据
| id | uid | title |
| --- | --- | --- |
| 1 | 1 | 文章标题 |
| 2 | 1 | 文章标题 |
| 3 | 1 | 文章标题 |
user表的数据
| uid | username |
| --- | --- |
| 1 | admin |
| 2 | xxxx |
| 3 | dddd |
问题: 如果获取 post表数据的时候 同时获取 用户名 (user.username)
答: 简单的都是先获取了 post的数据出来 再循环user表中的username
我们取出post表的数据时 还能取出uid 用户ID 却不能取出用户名,
这时就能用到多表查询
~~~
S('Post')->select(array(
"[>]user" => [ "uid" => "uid"], //post.uid == user.uid
),array(
'post.title',
'user.username'
)
);
输出:
arrary(
'title'=>'文章标题',
'username'=>'admin'
)
~~~
多表查询 Count
~~~
S('Post')->count(array(
"[>]user" => [ "uid" => "uid"], //post.uid == user.uid
),
'*'
);
~~~
复杂的多表查询 Count (HYBBS处的一段搜索代码)
~~~
$page_count = $Thread->count(
array(
"[>]post" => [ "pid" => "id"], //post.id == thread.pid
),
'*',
array('AND'=>array(
'isthread'=>1
,'OR'=>array(
'thread.title[~]'=>$key,
'post.content[~]'=>$key
)))
);
~~~
OBJ->count($join, "*" ,条件 );
- HYPHP框架前言
- 基本认识
- 环境要求
- 框架目录结构
- 入口文件
- 开始使用
- 访问框架
- 控制器
- 控制器定义
- 空控制器空函数
- 显示模板
- 模板变量赋值
- Json输出
- URL缩短控制器方法
- URL生成
- 请求类型
- 模板
- 建立模板
- 模板分组
- ----标签类----
- 输出变量
- 包含模板
- 判断标签
- 循环标签
- 数据库模型
- 数据库介绍
- 连接数据库
- 多数据库
- 配置多数据库
- 使用多数据库
- where条件
- 基础where
- AND OR
- LINK 模糊查找
- ORDER 排序
- MATCH 多键搜索
- 数据库内置函数
- LIMIT
- Select
- 多表Select
- Insert 插入数据
- Update 更新数据
- Delete 删除数据
- Find 查找一行
- Has 是否存在该数据
- Count 总数
- Max/Min 表最大/小值
- Action 事务
- 原生查询
- Model 定义
- 调试模式
- 查询缓存
- 自定义类库
- 框架内置函数
- X 获取预定义变量
- S 与 M 函数
- C 获取config配置
- A 调用另一个控制器方法
- cookie 与 session 函数
- E 函数
- cache 数据缓存
- F 文件数据缓存
- 判断电脑端移动端
- vendor 映射自动加载类路径
- 框架内置常量
- 框架内置Config.php配置
- 插件模式
- 开启插件模式
- 新建插件
- 插件独立开关
- 中文PHP
- 简介
- 开启支持
- 中文API
- 使用示例
- 加入自定义中文函数配置