thinkPHP3.2 中join用法執行個體

來源:互聯網
上載者:User
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.