MySQL查询 - 选择所有帖子并计算每个帖子的评论

时间:2012-10-31 07:42:39

标签: php mysql zend-framework zend-db zend-db-select

凌晨3:30在我的国家,所以我需要睡觉,但我不能没有这个:

我正在尝试获取所有帖子(使用Zend_Db)并为每个帖子计算评论。

模式

blog_posts

+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title         | varchar(255)     | NO   |     | NULL    |                |
| content       | text             | NO   |     | NULL    |                |
| alias         | varchar(100)     | NO   |     | NULL    |                |
| user_id       | int(11)          | NO   |     | NULL    |                |
| created_date  | datetime         | NO   |     | NULL    |                |
| modified_date | datetime         | YES  |     | NULL    |                |
| thumbnail     | varchar(255)     | YES  |     | NULL    |                |
+---------------+------------------+------+-----+---------+----------------+

这里是blog_comments

+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| user_id       | int(11)          | NO   |     | NULL    |                |
| post_id       | int(11)          | NO   |     | NULL    |                |
| comment       | text             | NO   |     | NULL    |                |
| created_date  | datetime         | NO   |     | NULL    |                |
| modified_date | datetime         | YES  |     | NULL    |                |
+---------------+------------------+------+-----+---------+----------------+

注意:blog_comments.post_idblog_posts.id相关联。


我想要一个像这样的结果表:

+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title         | varchar(255)     | NO   |     | NULL    |                |
| content       | text             | NO   |     | NULL    |                |
| alias         | varchar(100)     | NO   |     | NULL    |                |
| user_id       | int(11)          | NO   |     | NULL    |                |
| created_date  | datetime         | NO   |     | NULL    |                |
| modified_date | datetime         | YES  |     | NULL    |                |
| thumbnail     | varchar(255)     | YES  |     | NULL    |                |
+---------------+------------------+------+-----+---------+----------------+
| TEMPOROARY COLUMN IN OBJECT ($post->comment)                             |
+---------------+------------------+------+-----+---------+----------------+
| comments      |                  |      |     |         |                |
+---------------+------------------+------+-----+---------+----------------+

现在,这是我现在的查询:

SELECT `p`.*, `c`.*
FROM `blog_posts` `p`
LEFT JOIN (
    SELECT COUNT(*) 
    FROM `blog_comments` `c`
    WHERE c.post_id = p.id
) ON `p`.`comments`;

但它给了我一个错误:

Error Code: 1248. Every derived table must have its own alias

所以,如果有人可以帮助我,我们将非常感激!


重要提示 我正在使用Zend_DbZend_Db_Select,因此我必须能够使用joinLeft()或我需要的任何功能。

这是我select()的模型:

$select = $this->table->select();

if ($alias) {
    $select->where('alias = ?', $alias);
    return $this->table->fetchRow($select);
}
if ($withComments) {
    // I WILL PLACE THE CODE HERE, EXEMPLE:
    $select->joinLeft(...);
}

1 个答案:

答案 0 :(得分:3)

SELECT p.*, x.*
FROM blog_posts p
LEFT JOIN 
(
    SELECT post_id, COUNT(*) as cc
    FROM blog_comments
    GROUP BY post_id
) x 
ON x.post_id = p.id;