Home  >  Article  >  Backend Development  >  Code example for implementing multi-table related query in thinkphp

Code example for implementing multi-table related query in thinkphp

黄舟
黄舟Original
2017-10-13 09:27:181535browse

This article mainly introduces relevant information about the detailed examples of multi-table related queries in thinkphp. I hope this article can help everyone understand and master this part of the content. Friends in need can refer to it

Detailed explanation of examples of multi-table related queries in thinkphp

When programming back-end management systems, frameworks are generally used to quickly build pages. I have used it more recently. It is the thinkphp framework. The application of the thinkphp framework actually separates the front-end and the back-end for management. The front-end user login query system is placed in the home folder in thinkphp for management, and the back-end management system is placed in the admin folder in thinkphp. manage. By the way, when using the thinkphp framework, you need to use the MVC architecture. The MVC architecture is the structure of model (data model), view (view), and controller (controller). The interface here is controlled by the view. Control The function of the controller is to manage views and controllers. For detailed structure, you can consult the thinkphp documentation to learn.

What I’m going to talk about today is the correlation query application of database tables encountered in the back-end management system.

The first thing I want to talk about is the application of query statements in thinkphp. Of course, this is not a simple query of a data table, but a related query between multiple tables. Here There are two methods for data association: join and table.

1. First of all, the table method is introduced to perform related queries between multiple tables.

The first M is the M model in thinkphp , the function is to select the data table in the database, the table selects which tables to be associated, where is to perform conditional association, the function of field is actually a filtering function, which can output the information you want or useful, so After correlating the tables, the result is the data structure you want.


public function orderList(){
   $User=M("t_order");
   /*两个表的关联查询得到等级值*/
   $userinfo = $User->table('t_order,t_commodity,t_user')->where('t_order.cname = t_commodity.cname and t_order.uname = t_user.uname ')->field('t_order_id,t_order.orderid,t_order.cname')->select();
   $this->assign("userInfo",$userinfo);
   $this->display("order-list");
}

2. The join method performs related queries between multiple tables

The tables in this The difference between the associations is that join uses on to associate the tables. The rest is actually the same. Where is the association condition, and field is the filtered information (useful information for the next step)


public function getBanner(){
 $bannerid=1;
 $banner=M("banner_item");
 $result=$banner->join('image ON banner_item.img_id = image.id')->where("banner_item.banner_id=".$bannerid)->field("key_word,type,banner_id,url,from")->select();
 echo json_encode($result);
}

The above is the detailed content of Code example for implementing multi-table related query in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn