thinkPHP3.2 中join用法實例

小云云
發布: 2023-03-21 06:36:02
原創
4192 人瀏覽過

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
登入後複製

相關推薦:

實例詳解javaScript中split與join的差異

JavaScript把陣列中的所有元素放入一個字串的方法join( )

php join函數的使用程式碼實例

#

以上是thinkPHP3.2 中join用法實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!