Home > Backend Development > PHP Tutorial > Detailed explanation of multi-table related query in thinkphp

Detailed explanation of multi-table related query in thinkphp

小云云
Release: 2023-03-19 10:48:02
Original
2011 people have browsed it

This article mainly introduces relevant information on 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 who need it can refer to it. I hope it can help everyone.

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 The most commonly used framework recently 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 managed in the home folder of thinkphp, and the back-end management system is placed in the home folder of thinkphp. Manage in the admin folder. 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");
}
Copy after login

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);
}
Copy after login

Related recommendations:

Detailed explanation of the use of multi-table associations in YII2

join and joinwith multiple tables in Yii2 How to use related queries

Code examples for implementing multi-table related queries in thinkphp

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template