This article mainly introduces the usage of associated queries in ThinkPHP. The example analyzes the usage of table, join, native query, and multi-table query in ThinkPHP, which has some reference value, if you need it, refer to the example in this article to describe the usage of join queries in ThinkPHP. Share it with you for your reference. The specific analysis is as follows:
You can use the table () method or join method for join queries (Multi-table queries) in THINKPHP, as shown in the following example:
1. table ()
The Code is as follows:
$ List = $ user-> table ('user _ status stats, user_profile profile ')-> where ('stats. id = profile. typeid')-> field ('stats. id as id, stats. display as display, profile. title as title, profile. content as content ')-> order ('stats. id desc ')-> select ();
2. join ()
The Code is as follows:
$ User = new Model ('user ');
$ List = $ user-> join ('Right JOIN user_profile ON user_stats.id = user_profile.typeid')-> select ();
3. Native Query
The Code is as follows:
$ Model = new Model ();
$ SQL = 'select. id,. title, B. content from think_test1 as a, think_test2 as B where. id = B. id '. $ map. 'order by. id '. $ sort. 'limit '. $ p-> firstRow. ','. $ p-> listRows;
$ VoList = $ Model-> query ($ SQL );
4. Multi-Table query
The Code is as follows:
$ Model-> field ('user. name, role. title ')-> table ('Think _ user, think_role role')-> limit (10)-> select ();
Or:
The Code is as follows:
$ Model-> field ('user. name, role. title ')-> table (array ('Think _ user' => 'user', 'Think _ role' => 'role')-> limit (10) -> select ();
I hope this article will help you design PHP programs based on the ThinkPHP framework.