4

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();
4

1 に答える 1

3

ついにできた、

     $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();
于 2012-08-31T06:08:41.717 に答える