Thinkphp takes many learning notes for table queries and thinkphp takes notes. Thinkphp has many learning notes for table queries. during the operation of thinkphp's learning notes, there are no problems with the two table queries, but the three table queries start to show problems, sub-tables are as many as thinkphp learning notes for table queries and thinkphp learning Notes
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.
THINKPHP multi-table query
Two methods:
Multi-table query:
$ List = M ()-> table (array ('think _ Select' => 'this0', 'think _ student '=> 'this1 ', 'think _ class' => 'this2 '))
-> Where ('this0. stu_id = this1.id and this0.class _ id = this2.id ')
-> Field ('this0. id this0_id, this1.id this1_id, this2.id this2_id ')-> select ();
Generate SQL:
Select this0.id this0_id, this1.id this1_id, this2.id this2_id
From think_select this0, think_student this1, think_class this2
Where this0.stu _ id = this1.id and this0.class _ id = this2.id
Link query:
$ List = M ()-> table ("think_select this0")-> join ('think _ student this1 on this0.stu _ id = this1.id ')
-> Join ('think _ class this2 on this0.stu _ id = this2.id ')
-> Field ('this0. id this0_id, this1.id this1_id, this2.id this2_id ')-> select ();
Generate SQL:
Select this0.id this0_id, this1.id this1_id, this2.id this2_id
From think_select this0 left join think_student this1 on this0.stu _ id = this1.id
Left join think_class this2 on this0.stu _ id = this2.id
How to write thinkphp multi-table queries?
View model required
As follows: Class ArticleViewModel extends ViewModel {
Public $ viewFields = array (
'Article' => array ('id', 'title', 'content', 'Key _ id', 'task _ id', 'aout','re ', 'otime', 'URL', 'correlation', 'c _ length', 'AD', '_ type' => 'left '),
'Task' => array ('_ on' => 'article. task_id = Task. ID', 'task', 'num', 'Able', 'User _ id', 'Lang ',' _ type' => 'left '),
'User' => array ('_ on' => 'task. user_id = User. ID', 'manage', 'alie', 'belong _ to', '_ type' => 'left '),
);
}
?>
For details, refer to the thinkphp manual View model section.
During the operation, the two tables have no query problems, but the three tables have the following problems: the table Sharding is...