1

MySQL クエリを codeigniter 構文に変換する際に問題があります。

これは私のMySQLクエリです。

Select id, name, code, status, question_count, session
from s3r_set  
left join 
 (Select set_id, count(id) as question_count
    from  s3r_question group by set_id) question 
on question.set_id = id
left join
 (SELECT session_id as session, set_id
    from s3r_session) s3rsession
on s3rsession.set_id = id
order by id

これは私がこれまでCIで得たものです:

$this->db->select('id, name, code, status, question_count, session');
$this->db->from('s3r_set');
$this->db->join('s3r_question', '(Select set_id, count(id) as question_count
                                from  s3r_question group by set_id) question
                                on question.set_id = id', 'left');
$this->db->join('s3r_session', '(SELECT session_id as session, set_id
                                from s3r_session) s3rsession
                                on s3rsession.set_id = id', 'left');
$this->db->order_by('id', "desc");

$q = $this->db->get();

if($q->num_rows() > 0)
{
    return $q->result();
}
else
{
    return false;
}

事前に助けてくれてありがとう。

4

1 に答える 1

0

あなたがjoin間違っていることの1つは、次のとおりです。

$this->db->join('s3r_question', '(Select set_id, count(id) as question_count
                                from  s3r_question group by set_id) question
                                on question.set_id = id', 'left');

次のようにする必要があります。

$this->db->join('(Select set_id, count(id) as question_count 
from  s3r_question group by set_id) question', 'question.set_id = id');

ここでフォーマットを参照してください:
http://ellislab.com/codeigniter/user-guide/database/active_record.html#join

...join(FROM_TABLE, ON WHAT VALUE TO JOIN ON, OPTIONAL > WHAT TYPE OF JOIN, LEFT/RIGHT/ETC;)

于 2013-09-16T15:41:19.783 に答える