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();

$Model->join(array(' work ON artist.id = work.artist_id','card ON artist.card_id = card.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. python 全栈开发,Day62(外键的变种(三种关系),数据的增删改,单表查询,多表查询)

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

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

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

  3. THINKPHP 中关联查询(多表查询)

    THINKPHP 中关联查询(多表查询)可以使用 table() 方法或和join方法,请看示例: 1.Table方法:定义要操作的数据表名称,可以动态改变当前操作的数据表名称,需要写数据表的全名,包 ...

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

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

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

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

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

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

  7. MySQL 单表查询多表查询

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

  8. mysql数据库之单表查询多表查询

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

  9. mysql单表查询&&多表查询(职员表14+9)

    dept(deptno,dname,loc) emp(empno,ename,job,mgr,hiredate,sal,COMM,deptno) salgrade(grade,losal,hisal) ...

  10. Day055--MySQL--外键的变种,表与表的关系,单表查询,多表查询, 内连接,左右连接,全外连接

    表和表的关系 ---- 外键的变种 * 一对多或多对一 多对多 一对一 参考 https://www.cnblogs.com/majj/p/9169416.html 如何找出两张表之间的关系 分析步骤 ...

随机推荐

  1. ldr指令总结

    LDR/STR字和无符号字节加载/存储 1,LDR Rd,[Rn]   2, LDR Rd,[Rn,Flexoffset] 3, LDR Rd,[Rn],Flexoffset 4, LDR Rd,la ...

  2. uva 10047 the monocyle (四维bfs)

    算法指南白书 维护一个四维数组,走一步更新一步 #include<cstdio> #include<cstring> #include<queue> #includ ...

  3. [LeetCode] 86. Partition List 解题思路

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  4. TabHost自定义外观

    博客园:http://www.cnblogs.com 农民伯伯: http://www.cnblogs.com/over140 版本 新浪微博 weibo_10235010.apk 正文 一.效果图 ...

  5. yii CListView中使用CArrayDataProvider自定义数组作为数据

    CArrayDataProvider类手册: http://www.yiichina.com/api/CArrayDataProvider 在yii中无论是CListView还是CGridView,对 ...

  6. Android之ContextMenu的使用方法以及与OptionMenu的区别

    >> ContextMenu是android的context menu上下文菜单,选择某项VIEW后长按menu键,就会显示出来.比如EditeText就可以通过长按来弹出拥有“cut”, ...

  7. Codeforces Round #219 (Div. 2) E. Watching Fireworks is Fun

    http://codeforces.com/contest/373/problem/E E. Watching Fireworks is Fun time limit per test 4 secon ...

  8. EasyUI-动态添加tab

    layout效果 代码: <%@ page language="java" contentType="text/html; charset=utf-8" ...

  9. android 60 发送短信

    import android.os.Bundle; import android.app.Activity; import android.telephony.SmsManager; import a ...

  10. cocos2dx游戏项目总结(持续更新)

    1.在一个layer里面,尽量只使用一种按钮类型.如MenuItem or CCControlButton.因为这两种按钮的优先级不同,在使用过程中会互相影响到事件触发的先后顺序. 2.细节的问题要一 ...