微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – zend select joinleft中的查询

如何在Zend模型中的JOIN LEFT中编写SELECT查询
例如,如何将以下mysql查询转换为zend模型查询

    LEFT JOIN 
    (SELECT count(*) as game_count,topic_id,time as g_time from games_list WHERE type < 3 GROUP BY topic_id) t3
    ON chapter_list.id=t3.topic_id

我想用以下的zend查询添加转换后的查询,我需要从上面的查询中得到game_count,结果如下.

    $query = $this->select()

    ->setIntegrityCheck(false)

    ->from(array('a' => 'chapter_list'), array('subtopic_id as topic','id as topic_id','grade','chapter_no as chapter','if(file_status=1,1,0) as ppt)','CONCAT("http://mysite.com?t=",subtopic_id) as link'))

    ->joinLeft(array('b' => 'subject_list'), 'a.subject = b.subject_id', array());

    return $this->fetchAll($query)->toArray();

解决方法:

终于我明白了,

     $subquery1= $this->select()

    ->setIntegrityCheck(false)

    ->from('games_list', array('count(*) as game_count','topic_id','time as g_time'))

    ->where('type<?', 3)

    ->group('topic_id');

     //main query
     $query = $this->select()

    ->setIntegrityCheck(false)

    ->from(array('a' => 'chapter_list'), array('subtopic_id as topic','id as topic_id','grade','chapter_no as chapter','if(file_status=1,1,0) as ppt)','CONCAT("http://eskool.com/learn?t=",subtopic_id) as link'))

    ->joinLeft(array('b' => 'subject_list'), 'a.subject = b.subject_id', array())

    ->joinLeft(array('c' => $subquery1), 'a.id = c.topic_id', array('IFNULL(game_count,0)','g_time'))

    return $this->fetchAll($query)->toArray();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐