Examples of Multi-table join queries in thinkphp and thinkphp
Examples of Multi-table join queries in thinkphp
When programming the backend management system, the framework is usually used to quickly set up pages. Recently I have used thinkphp framework, the application of the thinkphp framework is to split the front-end and 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 managed in the admin folder of thinkphp. By the way, when using the thinkphp framework, the mvc Architecture is used. The mvc Architecture is the structure of model (Data model), view (view), controller (controller, the interface is controlled by the view. The controller is used to manage the view and controller. The detailed structure can be used to query thinkphp documents for learning.
What we want to talk about today is the associated query application for database tables in the backend management system.
The first thing to talk about is the application of the query statement in thinkphp. Of course, it is not a simple query of a data table, but an association query between multiple tables, there are two join and table methods for data association.
1. First, we will introduce the table method for multi-table join queries.
M is the M model in thinkphp. It is used to select data tables in the database, which tables are associated with, and where is used for conditional Association, the role of field is actually a filtering function, which can output the desired or useful information. In this way, after the table is joined, the result is the desired data structure.
Public function orderList () {$ User = M ("t_order "); /* obtain the level from the association query of the two tables */$ 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. join queries are performed between multiple tables.
The difference between the following table associations is that join uses on for table Association. The rest is actually the same, where is the join condition, field is the filtered information (useful for the following information)
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);}
If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!