ThinkPHP联表查询/关联查询

ThinkPHP联表查询/关联查询/多表查询可以使用 table() 方法或和join方法:

原生查询:

  1. $Model = new Model();  
  2. $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;  
  3. $list = $Model->query($sql);  


1、table()

  1. $result = M()->table(array('表1'=>'表1缩写','表2'=>'表2缩写'))->field('表1缩写.字段1,表2缩写.字段2')->where('表2.wb_id=表1.id')->select()  


2、join()

  1. $user = M('user')->join('RIGHT JOIN 表1 ON 表2.wb_id=表1.id' );  
  2. 例如:$user = M('user')->join('RIGHT JOIN user_profile ON user_stats.id = user_profile.typeid' );  


本文最后更新于 2014-03-16 19:33:21 并被添加「sql语句 thinkphp php框架」标签,已有 5103 位童鞋阅读过。
本文作者:未来往事
本站使用「署名 4.0 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处

相关文章

此处评论已关闭