In the process of learning thinkphp, You need to operate on multiple tables. But in the actual process, you always encounter various problems. Therefore, write this article as your learning process.
In the process of learning thinkphp, You need to operate on multiple tables. But in the actual process, you always encounter various problems. Therefore, write this article as your learning process.
During the operation, the two table queries are normal, but the three table queries start to cause problems.
There are three tables, including the pl table (uid, content), user table (id, username), and lyb table (uid, title)
You can use the following methods to query multiple tables:
(I) view model (recommended)
Define the view Model. You only need to inherit Think \ Model \ ViewModel and set the viewFields attribute.
Public $ viewFields = array ('pl '=> array ('uid', 'rid', 'content'), 'user' => array ('id ', 'username', '_ on' => 'pl. uid = user. id '), 'lyb' => array ('uid' => 'lid', 'content' => 'lyb _ content', 'title ', '_ on' => 'pl. uid = lyb. uid'), // If the table contains fields with duplicate names, you can set the alias through =>, 'uid' => 'lid ');
View query:
View query is the same as query of different models.
$ Model = D ("pl")-> field ('uid, title, username, lyb_content ')-> select (); // pl indicates the Database Name
If duplicate data exists in the query results, you can use the group method to process the data.
(Ii) join
The JOIN method is also one of the coherent operation methods. It is used to query data from these tables based on the relationship between columns in two or more tables.
Join Operations usually have the following types. Different types of join operations affect the returned data results.
Inner join: If the table has at least one match, the returned row is equivalent to JOIN.
Left join: returns all rows from the LEFT table even if no match exists in the right table.
Right join: returns all rows from the RIGHT table even if no match exists in the left table.
Full join: if one of the tables matches, the row is returned.
The join method supports the following four types:
Perform operations on the preceding 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 ();
(3) table
The table method is also one of the coherent operation methods of the model class. It is mainly used to specify the data table for the operation.
Usage
Generally, the system can automatically identify the corresponding data table when operating the model. Therefore, the table method is generally used:
The data table for the switchover operation;
Operate 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.
Articles you may be interested in:
Common Methods for Combined Query of multiple tables in ThinkPHP