【发布时间】:2011-12-17 16:21:25
【问题描述】:
mysql> describe posts;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | | NULL | |
| title | varchar(255) | NO | | NULL | |
| body | text | YES | | NULL | |
| category_id | int(11) | NO | | NULL | |
| tags | varchar(50) | NO | | NULL | |
| mark | tinyint(4) | NO | | 1 | |
| created | datetime | YES | | NULL | |
| modified | datetime | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
mysql> describe comments;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| post_id | int(11) | NO | MUL | NULL | |
| name | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | | NULL | |
| body | varchar(500) | NO | | NULL | |
| mark | tinyint(4) | NO | | 1 | |
| created | datetime | YES | | NULL | |
| modified | datetime | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
我需要具有更多 cmets 限制 10 或没有限制的 posts.title。
到目前为止我尝试了什么=>
$conditions=array(
'fields'=>array('Comment.post_id'),
'group'=>array('Comment.post_id'),
'order'=>array('count(Comment.post_id) DESC'),
'limit'=>'5'
);
$mostComments=$this->Post->Comment->find('all',$conditions);
$postId=array();
foreach($mostComments as $val){
array_push($postId,$val['Comment']['post_id']);
}
$postsWithmostComments=$this->Post->find('all',array('conditions'=>array('Post.id '=>$postId)) );
$this->set('postsWithmostComments',$postsWithmostComments);
任何人都可以发布 sql 查询以查找带有更多 cmets 的 posts.it、posts.title 吗?还是有 cakephp 的 find 命令?
【问题讨论】:
标签: mysql database cakephp cakephp-2.0