码迷,mamicode.com
首页 > 数据库 > 详细

YII2-数据库数据查询方法,关联查询with, joinWith区别和分页

时间:2017-01-06 22:01:59      阅读:490      评论:0      收藏:0      [点我收藏+]

标签:left join   pre   区别   ati   sel   分页查询   order by   yii   数据库数据   

一、ActiveRecord 活动记录

1.with关联查询

  例如,查询评论

 $post = Post::find()->with(‘comments‘);

 

  等价于以下结果集

 SELECT * FROM `post`;
 SELECT * FROM `comment` WHERE post_id IN (...)
 
 结合Pagination分页查询OK

 

2.joinWith关联查询,返回

 $post = Post::find()->joinWith(‘comments‘, true)

 等价于:

 SELECT * FROM post LEFT JOIN `comment` comments ON post.`id` = comments.`post_id`;
 SELECT * FROM `comment` WHERE post_id IN (...);
  
 joinWith 返回一个包含关联表的结果集, 可以使用 order by / 过滤条件 / group by 关联数据

  但joinWith 结合Pagination分页查询,只能返回1条记录,为何???

YII2-数据库数据查询方法,关联查询with, joinWith区别和分页

标签:left join   pre   区别   ati   sel   分页查询   order by   yii   数据库数据   

原文地址:http://www.cnblogs.com/LoveJulin/p/6257020.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!