程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> thinkphp學習筆記之多表查詢,thinkphp學習筆記之

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

編輯:關於PHP編程

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 多表查詢怎寫?

要用到 視圖模型

如下<?php
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 手冊 視圖模型部分
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved