Related query examples in ThinkPHP, ThinkPHP related examples
The examples in this article describe the usage of related queries in ThinkPHP. Share it with everyone for your reference. The specific analysis is as follows:
In THINKPHP, associated queries (multi-table queries) can use the table() method or the join method, as shown in the following example:
1. table()
Copy code 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()
Copy code 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
Copy code The code is as follows:
$Model = new Model();
$sql = 'select a.id,a.title,b.content from think_test1 as a, think_test2 as b where a.id=b.id '.$map.' order by a.id '.$sort.' limit '.$p->firstRow.','.$p->listRows;
$voList = $Model->query($sql);
4. Multi-table query
Copy code The code is as follows:
$Model->field('user.name,role.title')->table ('think_user user,think_role role')->limit(10)->select();
Or:
Copy code 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 be helpful to everyone’s PHP programming based on the ThinkPHP framework.
http://www.bkjia.com/PHPjc/920604.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/920604.htmlTechArticleExamples of associated queries in ThinkPHP, Examples of associated queries in ThinkPHP This article describes the usage of associated queries in ThinkPHP. Share it with everyone for your reference. The specific analysis is as follows: Related in THINKPHP...