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. 问题.spring源码转换为eclipse遇到的问题

    1.下载spring源码 2.下载安装gradle,配置环境变量 3.在spring子项目下执行命令:gradle cleanidea eclipse,会生成对应的.project及.classpat ...

  2. NLP(一)语料库和WordNet

    访问语料库 NLTK数据库的安装:http://www.nltk.org/data.html NLTK语料库列表:http://www.nltk.org/nltk_data/ 内部访问(以Reuter ...

  3. matplotlib 库的使用

    1.问题描述: 在学习kaggle经典学习项目Titanic,进行数据可视化处理时,对于每个特征进行相关性分析(也就是绘制pearson correlation heatmap )热力相关性矩阵时, ...

  4. 模板汇总——AC自动机

    AC自动机 模板题 HDU-2222 Keywords Search #include<bits/stdc++.h> using namespace std; #define LL lon ...

  5. JPA案例

    ORM 什么是ORM: 对象关系映射(Object Relational Mapping,简称ORM)是建立实体类和数据库表之间的关系,从而达到操作实体类就相当于操作数据库表的目的. ORM思想 主要 ...

  6. 《Ansible自动化运维:技术与佳实践》第一章读书笔记

    Ansible 架构及特点 第一章主要讲的是 Ansible 架构及特点,主要包含以下内容: Ansible 软件 Ansible 架构模式 Ansible 特性 Ansible 软件 Ansible ...

  7. 在React中使用Bootstrap

    这几天想在react中用一下bootstrap,尽管有一个适配react的很好的库叫react-bootstrap,但我还是想直接使用bootstrap 可以在react项目中执行以下命令安装boot ...

  8. C#客户端程序Visual Stadio远程调试

    一,需求来源 在开发过程中,可能会要使用Win7 ,Win8 ,Win10等不同版本的系统去做兼容性调试,也有时候会去针对特别的显卡,无线网卡等等硬件设备的机器做优化,有一种较优的方案,那就是使用Vi ...

  9. JAVA MAP转实体

    public static <T> T map2Object(Map<String, Object> map, Class<T> clazz) { SimpleDa ...

  10. Redis小白入门系列

    一.从NoSQL说起 NoSQL 是 Not only SQL 的缩写,大意为"不只是SQL",说明这项技术是传统关系型数据库的补充而非替代.在整个NoSQL技术栈中 MemCac ...