Home  >  Article  >  Backend Development  >  thinkphp multi-table query_PHP tutorial

thinkphp multi-table query_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:22:081014browse

thinkphp multi-table query

In the process of learning thinkphp, I need to operate multiple tables, but in the actual process, I always encounter various problems, so I wrote this blog post as my own learning process.
During the operation, there were no problems with the two-table query, but problems began to occur with the three-table query
There are the following three tables, the sub-tables are pl table (uid, content), user table (id, username), lyb table (uid, title)
There are several methods for multi-table query operations:
㈠View Model (recommended)
To define the view model, you only need to inherit ThinkModelViewModel and then set the viewFields attribute
Copy code
public $viewFields = array(
'pl' =>array('uid','rid','content'),
'user' =>array('id','username','_on'=>'pl.uid=user.id'),
> ), //If there are fields with the same name in the table, you can set the alias through =>, 'uid'=>'lid'
);
Copy code
View query:
View queries are the same as queries for different models, there is no difference.
$Model = D("pl") ->field('uid,title,username,lyb_content')->select(); //pl is the database name
If you find that there is duplicate data in the query results, you can also use the group method to process it.
㈡join
The JOIN method is also one of the coherent operation methods, used to query data from two or more tables based on the relationship between columns in these tables.
Joins usually have the following types. Different types of join operations will affect the returned data results.
INNER JOIN: Return rows if there is at least one match in the table, equivalent to JOIN
LEFT JOIN: Returns all rows from the left table even if there is no match in the right table
RIGHT JOIN: Returns all rows from the right table even if there is no match in the left table
FULL JOIN: Return rows as long as there is a match in one of the tables
The join method can support the above four types:
The same operation is performed on the above three tables
$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
The table method is also one of the coherent operation methods of the model class, and is mainly used to specify the data table for operation.
Usage
Generally, the system can automatically identify the current corresponding data table when operating the model, so the table method is usually used for:
Data table for switching operations;
Perform operations on multiple tables;                                                                                                   
$Model = D("pl")
->field('pl.content,user.username,lyb.title')
->table('pl,lyb,user')
 ->limit(10)
->select();
Note: The table method queries the values ​​of all fields by default

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/851376.htmlTechArticlethinkphp multi-table query In the process of learning thinkphp, you need to operate multiple tables, but in the actual process, I always encounter various problems, so I wrote this blog post as my own learning...
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