In the process of learning thinkphp, need to operate on more than one table, but in the actual process, always encounter a variety of problems, so write this blog, as their learning process in the course of the operation, the two tables query is no problem, but the three table query began to appear problems have the following three tables , the table is a PL table (uid,content), User table (id,username), Lyb table (uid,title) Multiple table query operations have the following methods: ㈠ view model (recommended) Define the view model, You only need to inherit Think\model\viewmodel and then set the ViewFields property to copy code public $viewFields = Array ( ' PL ' &nb Sp =>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 has duplicate names, you can set aliases by =>, ' uid ' => ' lid ' ); Copy Code view queries: View queries are no different from the query of the various models. $Model = D ("pl")->field (' Uid,title,username,lyb_content ')->select (); //PL for database names You can also use the group method if you find duplicate data in the results of a query. The ㈡join Join method is also one of the consecutive methods used to base the relationship between columns in two or more tablesQuery the data from these tables. joins usually have the following types, and different types of join operations affect the returned data results. INNER Join: If there is at least one match in the table, the row is returned, equivalent to the join LEFT join: All row right joins are returned from the left table, even if there is no match in the right-hand table: All row full joins are returned from the right table even if there is no match in the left table To have a match in one of the tables, return the row join method to support the above four types: also operates on the above three tables $Model = D ("pl")->join (' lyb on pl.uid = lyb.u Id ')->join (' user on pl.uid = User.ID ') ->field (' user.username,lyb.title,pl.content ') ->select (); The ㈢table table method is also part of a coherent operation of the model class, which is used primarily to specify the data table for the operation. Usage in general, the system can automatically recognize the current data table when the model is operated, so the table method is usually used to: The data table of the switching operation; & nbsp &NB Sp &NB Sp &N Bsp $Model D ("pl")->field (' Pl.content,user.username,lyb.title ')->table (' Pl,lyb,user ')->limit ()->select (); Note: Table method default query is the value of all fields
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.