thinkphp學習筆記之多表查詢,thinkphp學習筆記之_PHP教程

來源:互聯網
上載者:User

thinkphp學習筆記之多表查詢,thinkphp學習筆記之


在操作過程中,兩表查詢都沒有問題,但是三表查詢就開始出現問題

有以下三張表,分表為pl表(uid,content),user表(id,username),lyb表(uid,title)

多表查詢操作有以下幾種方法:

㈠視圖模型(推薦)

定義視圖模型,只需要繼承Think\Model\ViewModel,然後設定viewFields屬性即可

public $viewFields = array(    'pl'    =>array('uid','rid','content'),    'user'    =>array('id','username','_on'=>'pl.uid=user.id'),    'lyb'    =>array('uid'=>'lid','content'=>'lyb_content','title','_on'=>'pl.uid=lyb.uid'), //如果表中有欄位重名,可以通過=>設定別名,'uid'=>'lid'    );   

視圖查詢:

視圖查詢和不同模型的查詢一樣,沒有什麼區別。

$Model = D("pl") ->field('uid,title,username,lyb_content')->select();  //pl為資料庫名

如果發現查詢的結果存在重複資料,還可以使用group方法來處理。

㈡join

JOIN方法也是連貫操作方法之一,用於根據兩個或多個表中的列之間的關係,從這些表中查詢資料。

join通常有下面幾種類型,不同類型的join操作會影響返回的資料結果。

INNER JOIN : 如果表中有至少一個匹配,則返回行,等同於 JOIN
LEFT JOIN : 即使右表中沒有匹配,也從左表返回所有的行
RIGHT JOIN : 即使左表中沒有匹配,也從右表返回所有的行
FULL JOIN : 只要其中一個表中存在匹配,就返回行
join方法可以支援以上四種類型:

同樣是對以上三張表進行操作

$Model = D("pl")          ->join('lyb on pl.uid = lyb.uid')          ->join('user on pl.uid = user.id')           ->field('user.username,lyb.title,pl.content')          ->select();

㈢table

table方法也屬於模型類的連貫操作方法之一,主要用於指定操作的資料表。

用法

一般情況下,操作模型的時候系統能夠自動識別當前對應的資料表,所以,使用table方法的情況通常是為了:

切換操作的資料表;
對多表進行操作;

$Model = D("pl")  ->field('pl.content,user.username,lyb.title')  ->table('pl,lyb,user')  ->limit(10)  ->select();

註:table方法預設查詢的是所有欄位的值


THINKPHP多表查詢問題

二種方法:
多表查詢:

$list=M()->table(array('think_select'=>'this0','think_student'=>'this1','think_class'=>'this2'))
->where('this0.stu_id=this1.id and this0.class_id=this2.id')
->field('this0.id this0_id,this1.id this1_id,this2.id this2_id')->select();
產生sql:
select this0.id this0_id,this1.id this1_id,this2.id this2_id

from think_select this0,think_student this1,think_class this2
where this0.stu_id=this1.id and this0.class_id=this2.id

連結查詢:

$list=M()->table("think_select this0")->join('think_student this1 on this0.stu_id=this1.id')
->join('think_class this2 on this0.stu_id=this2.id')
->field('this0.id this0_id,this1.id this1_id,this2.id this2_id')->select();
產生sql:
select this0.id this0_id,this1.id this1_id,this2.id this2_id

from think_select this0 left join think_student this1 on this0.stu_id=this1.id
left join think_class this2 on this0.stu_id=this2.id
 

thinkphp 多表查詢怎寫?

要用到 視圖模型

如下class ArticleViewModel extends ViewModel{
public $viewFields = array(
'Article' => array('id','title','content','key_id','task_id','aout','re','otime','url','correlation','c_length','ad','_type'=>'left'),
'Task' => array('_on'=>'Article.task_id=Task.id','task','num','able','user_id','lang','_type'=>'left'),
'User' => array('_on'=>'Task.user_id=User.id','manage','alie','belong_to','_type'=>'left'),
);
}
?>
具體可以參考 thinkphp 手冊 視圖模型部分
 

http://www.bkjia.com/PHPjc/851350.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/851350.htmlTechArticlethinkphp學習筆記之多表查詢,thinkphp學習筆記之 在操作過程中,兩表查詢都沒有問題,但是三表查詢就開始出現問題 有以下三張表,分表為...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.