This article mainly introduces common methods of multi-table joint query in ThinkPHP, which is very important for project development! For more information, see join queries in ThinkPHP (multi-table join queries). you can use the table () method or join method, as shown in the following example:
1. native Query example:
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 );
2. join () method example:
The code is as follows:
$ User = new Model ('user ');
$ List = $ user-> join ('right JOIN user_profile ON user_stats.id = user_profile.typeid ');
3. example of the table () method:
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 ();