thinkphp Multi-Table query

Source: Internet
Author: User

thinkphp Multi-Table query processing

thinkphp Multi-table connection query processing

Thinkphp Association query (Multi-table query)


Three ways are found online: Table (), join (), native SQL statement query. (The following three methods output the same, and the thinkphp own paging function is well preserved)


First type: Table () method

Example: Two tables (table Agent and table Transinfo) that require connection query

$Model =new Model (); $sqlcount = "SELECT count (*) as mycount from Agent A, Transinfo t where T.clientid=a.id and T.transtype Like '%agent% ' and a.id in (". $agent _str.");  $listCount = $Model->query ($sqlcount), $Page = new Page ($listCount [0][mycount], 2); $show = $Page->show (); $list = $Model->table (' Agent A, transinfo T ')->where ("T.clientid=a.id and t.transtype like '%agent% ' and a.id in (". $agent _ Str. ")") ->limit ($Page->firstrow. ‘,‘ . $Page->listrows)->select ();//echo $Model->getlastsql (); $this->assign (' list ', $list);//Assignment DataSet $this- >assign (' page ', $show);//Assignment Paging output



Second type: Join () method

Example: Two tables (table Agent and table Transinfo) that require connection query


$Model =new model (), $agentModel  =  $Model->table ("Agent"), $listCount  =  $agentModel- >join (" as a right join transinfo t on t.clientid=a.id and  t.transType like  '%agent% '  and a.id in  (". $agent _str.") ->field ("Count (*)  as mycount")->select (); $Page  = new Page  ( $ listcount[0][mycount], 2 ); $show  =  $Page->show  (); $Model =new model (); $ agentmodel =  $Model->table ("Agent"), $list  = $agentModel->join (" as a right  JOIN transinfo t ON t.clientId=a.id and t.transType like  '%agent % '  and a.id in  (". $agent _str.")  order by t.id DESC limit  ". $Page->firstrow.", ". $Page->listrows)->field ("a.*,t.*")->select ();//echo  $agentModel->getlastsql (); $this->assign (' list ', $list);//  assignmentData set $this->assign (' page ', $show);//  assignment Paging output 

Tip: You can also instantiate more succinctly (officially recommended): $agentModel = M (' Agent '); Instantiating a User object



The third type: Native SQL statement Query method

$Model =new Model (); $sqlcount = "SELECT count (*) as mycount from Agent A, Transinfo t where T.clientid=a.id". $where. "and T.transtype like '%agent% ' and a.id in (". $agent _str."); $listCount = $Model->query ($sqlcount), $Page = new page ($listCount [0][' Mycount '],2);//Instantiate the number of incoming total records for a paging class and the number of records displayed per page $show =  $Page->show (); $sqlList = "Select T.*,a.* from Agent A, Transinfo t where T.clientid=a.id". $where. "and T.transtype like '%agent% ' and a.id in (". $agent _str.") Limit {$Page->firstrow},{$Page->listrows} "; $list = $Model->query ($ sqllist); $this->assign (' list ', $list);//Assignment Data Set $this->assign (' page ', $show);//Assignment Paging output




Resources:

Http://hi.baidu.com/wjlhh001/item/18d9a91031081b5e2a3e22af

Http://www.yunda51.com/304.html

Http://www.w3school.com.cn/sql/sql_join_right.asp



END



This article is from "Phper Xuchen-Focus on PHP Technology" blog, be sure to keep this source http://xuqin.blog.51cto.com/5183168/1564492

thinkphp Multi-Table query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.