3
我想在zend_db_select(ZF 1.12)中使用相關子查詢構建一個可用的MySql查詢,以便在Zend_Paginator_Adapter中使用它。工作查詢如下:MYSQL-在Zend中使用相關的子查詢Db
SELECT f.*, (SELECT (COUNT(p.post_id) - 1)
FROM `forum_topic_posts` AS p WHERE f.topic_id = p.topic_id) AS post_count
FROM `forum_topics` AS f WHERE f.forum_id = '2293'
ORDER BY post_count DESC, last_update DESC
所以我摸索出:
$subquery = $db->select()
->from(array('p' => 'forum_topic_posts'), 'COUNT(*)')
->where('p.topic_id = f.topic_id');
$this->sql = $db->select()
->from(array('f' => 'forum_topics'), array('*', $subquery . ' as post_count'))
->where('forum_id=?', $forumId, Zend_Db::PARAM_INT)
->order('post_count ' . $orderDirection);
但Zend公司與執行查詢時出現以下異常停止:
Zend_Db_Statement_Mysqli_Exception: Mysqli prepare error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT COUNT(*) FROM forum_topic_posts AS p WHERE (p.topic_id = f.to' at line 1
我怎麼能讓子查詢工作?
謝謝,這是完美的作品。想投票但是沒有足夠的聲望。 – DerFlow 2013-02-21 01:37:34