如何将给定的多重连接查询转换为 ZendFrame 工作查询

发布于 2024-11-17 15:15:46 字数 574 浏览 3 评论 0原文

SELECT
 `blg`.`id` ,
 `blg`.`heading` , 
 `blg`.`description` ,
 `cat`.`name` AS `categoryname` ,
 `mem`.`firstname` AS `user` ,
  ( SELECT COUNT( * ) FROM blog_comments WHERE blog_comments.status = '1' AND blog_comments.blogid = blg.id) AS `commentcount`
    FROM `blogs` AS `blg`
  INNER JOIN `blog_category` AS `cat` ON blg.category = cat.id
  INNER JOIN `members_member` AS `mem` ON blg.user = mem.id
  LEFT JOIN `blog_comments` AS `cmt` ON blg.id = cmt.blogid
  WHERE (        blg.status = '1'        )
  GROUP BY blg.id ORDER BY `blg`.`id` ASC
SELECT
 `blg`.`id` ,
 `blg`.`heading` , 
 `blg`.`description` ,
 `cat`.`name` AS `categoryname` ,
 `mem`.`firstname` AS `user` ,
  ( SELECT COUNT( * ) FROM blog_comments WHERE blog_comments.status = '1' AND blog_comments.blogid = blg.id) AS `commentcount`
    FROM `blogs` AS `blg`
  INNER JOIN `blog_category` AS `cat` ON blg.category = cat.id
  INNER JOIN `members_member` AS `mem` ON blg.user = mem.id
  LEFT JOIN `blog_comments` AS `cmt` ON blg.id = cmt.blogid
  WHERE (        blg.status = '1'        )
  GROUP BY blg.id ORDER BY `blg`.`id` ASC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那支青花 2024-11-24 15:15:46

像这样的东西怎么样:

        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $db->select();

        $dbExpr = new Zend_Db_Expr("(SELECT COUNT( * ) FROM blog_comments WHERE blog_comments.status = '1' AND blog_comments.blogid = blg.id) AS `commentcount`");        

        $select->from(
                    array('blg' => 'blogs'), 
                    array('id', 'heading', 'description', 'name as categoryname', $dbExpr)
               )->joinInner(
                            array('cat' => 'blog_category'), 
                            'blg.category = cat.id',
                            array()
               )->joinInner(
                            array('mem' => 'members_member'), 
                            'blg.user = mem.id',
                            array()
               )->joinLeft(
                            array('cmt' => 'blog_comments'), 
                            'blg.id = cmt.blogid',
                            array()
               )->where('blg.status = ?', '1')
                ->group('blg.id')
                ->order('blg.id ASC');


        echo $select->assemble();
        exit;

输出:

SELECT `blg`.`id`, `blg`.`heading`, `blg`.`description`, `blg`.`name` AS `categoryname`, 
(SELECT COUNT( * ) FROM blog_comments WHERE blog_comments.status = '1' AND blog_comments.blogid = blg.id) AS `commentcount`
FROM `blogs` AS `blg` 
INNER JOIN `blog_category` AS `cat` ON blg.category = cat.id
INNER JOIN `members_member` AS `mem` ON blg.user = mem.id 
LEFT JOIN `blog_comments` AS `cmt` ON blg.id = cmt.blogid 
WHERE (blg.status = '1') 
GROUP BY `blg`.`id` 
ORDER BY `blg`.`id` ASC

What about something like this:

        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $db->select();

        $dbExpr = new Zend_Db_Expr("(SELECT COUNT( * ) FROM blog_comments WHERE blog_comments.status = '1' AND blog_comments.blogid = blg.id) AS `commentcount`");        

        $select->from(
                    array('blg' => 'blogs'), 
                    array('id', 'heading', 'description', 'name as categoryname', $dbExpr)
               )->joinInner(
                            array('cat' => 'blog_category'), 
                            'blg.category = cat.id',
                            array()
               )->joinInner(
                            array('mem' => 'members_member'), 
                            'blg.user = mem.id',
                            array()
               )->joinLeft(
                            array('cmt' => 'blog_comments'), 
                            'blg.id = cmt.blogid',
                            array()
               )->where('blg.status = ?', '1')
                ->group('blg.id')
                ->order('blg.id ASC');


        echo $select->assemble();
        exit;

Output:

SELECT `blg`.`id`, `blg`.`heading`, `blg`.`description`, `blg`.`name` AS `categoryname`, 
(SELECT COUNT( * ) FROM blog_comments WHERE blog_comments.status = '1' AND blog_comments.blogid = blg.id) AS `commentcount`
FROM `blogs` AS `blg` 
INNER JOIN `blog_category` AS `cat` ON blg.category = cat.id
INNER JOIN `members_member` AS `mem` ON blg.user = mem.id 
LEFT JOIN `blog_comments` AS `cmt` ON blg.id = cmt.blogid 
WHERE (blg.status = '1') 
GROUP BY `blg`.`id` 
ORDER BY `blg`.`id` ASC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文