thinkPHP3.2 join 用法详解

inner join  如果表中至少 有一个匹配,在则返回行,等同与 join 。
left    join  即使 有右表中没有匹配 ,也从左表中返回所有的行。
right join  即使左表中没有匹配,也从右表中返回所有的行。
full    join  只要其中一个表 中匹配,就返回行。

 $lists = $this->orderModel
     ->alias('t')
     ->field('t.*,o.order_id as ccsid')
      ->join('left join __ORDER__ as o on t.order_id = o.third_order_id  and t.source = o.source')
      ->where($map)
      ->order("create_time DESC")
      ->limit($page->firstRow . ',' . $page->listRows)
      ->select();

Tp语法注释:
$lists = $this->orderModel              // M('third_order');
    ->alias('t')                        // 别名
    ->field('t.*,o.order_id as ccsid')  // 要查的字段
    ->join('left join __ORDER__ as o on t.order_id = o.third_order_id  and t.source = o.source')
    //关联表(左链接  order表 as o 别名  on关系 t.xx = o.xxx          and  t.xxx  =  o.xxx  )
    ->where($map)                       // 条件
    ->order("create_time DESC")         // 排序
    ->limit($page->firstRow . ',' . $page->listRows)  // 取几条
    ->select();

解析的sql 语句为:
            
     SELECT    t.*,o.order_id as ccsid 
     FROM    db_third_order t 
     left join   db_order as o 
     on  t.order_id = o.third_order_id and t.source = o.source 
     WHERE   t.status <> 0 
     ORDER BY  create_time DESC 
     LIMIT     0,20 

原生sql注释 :
select 表名(别名).* , 表名(别名). 字段 as 别名
from 主表 空格 别名(t)
left join 从表 as 别名 (o)
on   t.xxx = o.xxx   and   t.xxx = o.xxx   <表关系>
where  条件  
order by  字段名 desc   倒叙
limit  0,20









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值