THINKPHP 中关联查询(多表查询)可以使用 table() 方法或和join方法,请看示例:

1、Table方法:定义要操作的数据表名称,可以动态改变当前操作的数据表名称,需要写数据表的全名,包含前缀,可以使用别名,例如:

$Model->Table('think_user user')

->where('status>1')

->select();

$Model->table('think_blog blog,think_type type')
->where('blog.typeid=type.id')
->field('blog.id as id,blog.title,blog.content,type.typename as type')
->order('blog.id desc' )
->limit(5)
->select();

Table方法的参数支持字符串和数组,数组方式的用法:

$Model->Table(array('think_user'=>'user','think_group'=>'group'))

->where('status>1')

->select();

使用数组方式定义的优势是可以避免因为表名和关键字冲突而出错的情况。
 
注:如果不定义table方法,默认会自动获取当前模型对应或者定义的数据表。
 
2、Join方法:查询Join支持,Join方法的参数支持字符串和数组,并且join方法是连贯操作中唯一可以多次调用的方法。例如:

$Model->join('work ON artist.id = work.artist_id')

->join('card ON artist.card_id = card.id')

->select();

//Left Join
$Model->table('user U')

->join('news N on U.id=N.cid')

->field('U.*,N.*')

->order('id desc')

->limit('8')

->findall();

 
默认采用LEFT JOIN 方式,如果需要用其他的JOIN方式,可以改成

$Model->join('RIGHT JOIN work ON artist.id = work.artist_id')

->select();

//Right Join
$Model->table('user U')
->join(array('right','news N on U.id=N.cid'))
->field('U.*,N.*')
->order('id desc')
->limit('8')
->findall();

如果join方法的参数用数组的话,只能使用一次join方法,并且不能和字符串方式混合使用。

$Model->join(array(' work ON artist.id = work.artist_id', 'card ON artist.card_id = card.id'))

->select()

 
运用这种连贯操作方法,可以有效的提高数据查询的代码清晰度和开发效率。
 
查看连贯操作的SQL语句的方法:

echo $Model->getLastSql(); //打印一下SQL语句,查看一下

例2:
1、table()

$list = $user->table('user_status stats, user_profile profile')->where('stats.id = profile.typeid')->field('stats.id as id, stats.display as display, profile.title as title,profile.content as content')->order('stats.id desc' )->select();

2.1、join()2表查询

$user = new Model('user');

$list = $user->join('RIGHT JOIN user_profile ON user_stats.id = user_profile.typeid' );

2.2、join() 多表查询

        $list = $Form->join('think_sort ON think_form.sort_id = think_sort.sort_id' )->join('think_brand ON think_form.brand_id = think_brand.brand_id' )->select();

3、原生查询

$Model = new Model();

$sql = 'select a.id,a.title,b.content from think_test1 as a, think_test2 as b where a.id=b.id '.$map.' order by a.id '.$sort.' limit '.$p->firstRow.','.$p->listRows;
$voList = $Model->query($sql);

THINKPHP 中关联查询(多表查询)的更多相关文章

  1. Oracle中把一张表查询结果插入到另一张表中

      1. 新增一个表,通过另一个表的结构和数据 create table XTHAME.tab1 as select * from DSKNOW.COMBDVERSION 2. 如果表存在: inse ...

  2. thinkphp多层volist实现多表查询

    thinkphp多层volist实现多表查询 一.总结 二.截图 三.代码 1.控制器 2.视图

  3. python 全栈开发,Day62(外键的变种(三种关系),数据的增删改,单表查询,多表查询)

    一.外键的变种(三种关系) 本节重点: 如何找出两张表之间的关系 表的三种关系 一.介绍 因为有foreign key的约束,使得两张表形成了三种了关系: 多对一 多对多 一对一 二.重点理解如果找出 ...

  4. MySQL--详细查询操作(单表记录查询、多表记录查询(连表查询)、子查询)

    一.单表查询 1.完整的语法顺序(可以不写完整,其次顺序要对) (不分组,且当前表使用聚合函数: 当前表为一组,显示统计结果 ) select distinct [*,查询字段1,查询字段2,表达式, ...

  5. mysql中各种join连表查询总结

    通常我们需要连接多个表查询数据,以获取想要的结果. 一.连接可以分为三类: (1) 内连接:join,inner join (2) 外连接:left join,left outer join,righ ...

  6. django models的点查询/跨表查询/双下划线查询

    django models 在日常的编程中,我们需要建立数据库模型 而往往会用到表与表之间的关系,这就比单表取数据要复杂一些 在多表之间发生关系的情形下,我们如何利用models提供的API的特性获得 ...

  7. python mysql 单表查询 多表查询

    一.外键 变种: 三种关系: 多对一 站在左表的角度: (1)一个员工 能不能在 多个部门? 不成立 (2)多个员工 能不能在 一个部门? 成立 只要有一个条件成立:多 对 一或者是1对多 如果两个条 ...

  8. python开发mysql:单表查询&多表查询

    一 单表查询,以下是表内容 一 having 过滤 1.1 having和where select * from emp where id > 15; 解析过程;from > where ...

  9. MySQL 单表查询多表查询

    一 单表查询 表准备 create table emp( id int not null unique auto_increment, name varchar(20) not null, sex e ...

随机推荐

  1. NLP(四) 正则表达式

    * + ? * :0个或多个 + :1个或多个 ? :0个或1个 re.search()函数,将str和re匹配,匹配正确返回True import re # 匹配函数,输入:文本,匹配模式(即re) ...

  2. codeforces 877 E. Danil and a Part-time Job(线段树(dfs序))

    题目链接:http://codeforces.com/contest/877/problem/E 题解:显然一看就感觉要么树链剖分要么线段树+dfs序,题目要求的操作显然用线段树+dfs序就可以实现. ...

  3. Codeforces Round #582 (Div. 3)

    题目链接:https://codeforces.com/contest/1213 A: 题意:给定数的位置,位置为整数,每个数可以向左或右移动一格或者两格,移动一格花费一个硬币,两格不花费硬币,问所有 ...

  4. 【Windows】PostgreSql安装

    Installer安装包问题 Problem running post-install step. Installation may not complete correctly. The datab ...

  5. 实现一个基于码云的Storage

    实现一个简单的基于码云(Gitee) 的 Storage Intro 上次在 asp.net core 从单机到集群 一文中提到存储还不支持分布式,并立了一个 flag 基于 github 或者 开源 ...

  6. 解决subline安装插件被墙失败的方法

    一.问题场景描述 当你完成subline和package control的安装后,准备使用install package安装各种各样的插件来丰富你的编辑器,却出现类似 “Unable to downl ...

  7. Go语言基础之网络编程

    现在我们几乎每天都在使用互联网,我们前面已经学习了如何编写Go语言程序,但是如何才能让我们的程序通过网络互相通信呢?本章我们就一起来学习下Go语言中的网络编程. 关于网络编程其实是一个很庞大的领域,本 ...

  8. spring boot使用常规发送邮件

    spring boot使用常规发送邮件 1.pom.xml文件依赖: <!-- javax.mail begin--> <dependency> <groupId> ...

  9. idea中applicationContext-dao.xml文件中Cannot resolve file***** :spring xml model validation问题

    访问不了classpath下的文件夹中的文件 解决办法如下:(问题出在我创建的resources文件夹是一个普通的文件夹) 1.本来是普通的文件夹 2.ctrl+shift+alt+s打开如下界面: ...

  10. C# Post Get 方式发送请求

    httpPost 方式发送请求 不带参数 /// <summary> /// 没有参数的post请求 /// </summary> public void HttpPostNo ...