PHP分享
您现在的位置是:博客主页 > Web教程 > PHP分享

ThinkPHP5.1Db类两表,三表链表查询,阔以啊,一起把数据都取出来了

By Dianthink on PHP分享 Time 2019-06-28 from:互联网 205人已围观
简介:    ThinkPHP5.1版本的Db类: JOIN方法用于根据两个或多个表中的列之间的关系,从这些表中查询数据。join通常有下面几种类型,不同类型的join操作会影响返回的数据结果。 INNER JOIN: 等同于 JOIN(默认的JOIN类型),如果表中有至少一个匹配,则返回行 LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行 RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行 FULL JOIN: 只要其中一个表中存在匹配,就返回行

ThinkPHP5.1版本的Db类:

JOIN方法用于根据两个或多个表中的列之间的关系,从这些表中查询数据。join通常有下面几种类型,不同类型的join操作会影响返回的数据结果。
INNER JOIN: 等同于 JOIN(默认的JOIN类型),如果表中有至少一个匹配,则返回行
LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行
RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行
FULL JOIN: 只要其中一个表中存在匹配,就返回行

说明
join ( mixed join [, mixed $condition = null [, string $type = 'INNER']] )
leftJoin ( mixed join [, mixed $condition = null ] )
rightJoin ( mixed join [, mixed $condition = null ] )
fullJoin ( mixed join [, mixed $condition = null ] )
参数
join
要关联的(完整)表名以及别名
支持的写法:
写法1:[ '完整表名或者子查询'=>'别名' ]
写法2:'不带数据表前缀的表名'(自动作为别名)
写法2:'不带数据表前缀的表名 别名'
condition
关联条件。可以为字符串或数组, 为数组时每一个元素都是一个关联条件。
type
关联类型。可以为:`INNER`、`LEFT`、`RIGHT`、`FULL`,不区分大小写,默认为`INNER`。
返回值
模型对象
举例演示一下,因为表二中有admin_id 都为1所以打印出四条记录:

fa_admin表一

fa_admin_log表二

fa_auth_group_access表三

<?php
namespace appindexcontroller;
use thinkDb;
class Index
{
    public function index()
    {
		$list = Db::table('fa_admin')
		->alias('a')
		->join('fa_admin_log w','a.id = w.admin_id')
		->join('fa_auth_group_access c','a.id = c.uid')
		->select();
		print_r($list);
    }
} 

打印结果:

 Array
(
    [0] => Array
        (
            [id] => 1
            [username] => admin
            [nickname] => Admin
            [password] => 248b6c0d4124b8e28b702db245afd75e
            [salt] => 51fc85
            [avatar] => /assets/img/avatar.png
            [email] => admin@admin.com
            [loginfailure] => 0
            [logintime] => 1561386345
            [createtime] => 1558446498
            [updatetime] => 1561386345
            [token] => 3d7375e1-bf4b-4f14-8b75-ea45ada45b8a
            [status] => normal
            [admin_id] => 1
            [url] => /index/login.html
            [title] => 登录
            [content] => {"__token__":"5a6da4724495727c8ebb480ab8750f5f","username":"admin","keeplogin":"1"}
            [ip] => 127.0.0.1
            [useragent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
            [uid] => 1
            [group_id] => 1
        )

    [1] => Array
        (
            [id] => 2
            [username] => admin
            [nickname] => Admin
            [password] => 248b6c0d4124b8e28b702db245afd75e
            [salt] => 51fc85
            [avatar] => /assets/img/avatar.png
            [email] => admin@admin.com
            [loginfailure] => 0
            [logintime] => 1561386345
            [createtime] => 1561386346
            [updatetime] => 1561386345
            [token] => 3d7375e1-bf4b-4f14-8b75-ea45ada45b8a
            [status] => normal
            [admin_id] => 1
            [url] => /index/login.html
            [title] => 登录
            [content] => {"__token__":"1807fbbc18cdaadc1a303c9c513c49f2","username":"admin"}
            [ip] => 127.0.0.1
            [useragent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
            [uid] => 1
            [group_id] => 1
        )

    [2] => Array
        (
            [id] => 3
            [username] => admin
            [nickname] => Admin
            [password] => 248b6c0d4124b8e28b702db245afd75e
            [salt] => 51fc85
            [avatar] => /assets/img/avatar.png
            [email] => admin@admin.com
            [loginfailure] => 0
            [logintime] => 1561386345
            [createtime] => 1561386404
            [updatetime] => 1561386345
            [token] => 3d7375e1-bf4b-4f14-8b75-ea45ada45b8a
            [status] => normal
            [admin_id] => 1
            [url] => /addon/install
            [title] => 插件管理 安装
            [content] => {"name":"geetest","force":"0","uid":"0","token":"","version":"1.0.0","faversion":"1.0.0.20180911_beta"}
            [ip] => 127.0.0.1
            [useragent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
            [uid] => 1
            [group_id] => 1
        )

    [3] => Array
        (
            [id] => 4
            [username] => admin
            [nickname] => Admin
            [password] => 248b6c0d4124b8e28b702db245afd75e
            [salt] => 51fc85
            [avatar] => /assets/img/avatar.png
            [email] => admin@admin.com
            [loginfailure] => 0
            [logintime] => 1561386345
            [createtime] => 1561386406
            [updatetime] => 1561386345
            [token] => 3d7375e1-bf4b-4f14-8b75-ea45ada45b8a
            [status] => normal
            [admin_id] => 1
            [url] => /index/index
            [title] => 
            [content] => {"action":"refreshmenu"}
            [ip] => 127.0.0.1
            [useragent] => Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
            [uid] => 1
            [group_id] => 1
        )

) 
相关文章
文章评论

( 0 )