将此SQL查询转换为Codeigniter Active Record

时间:2014-02-08 05:15:18

标签: mysql codeigniter select activerecord

这是我需要转换的查询

SELECT pdg_matriculas.id_matricula, aluno.nome_aluno, pdg_serie.nome_serie, pdg_turma.nome_turma, aluno.status_aluno, pdg_matriculas.ano_letivo
FROM pdg_matriculas, pdg_serie, pdg_turma, aluno
WHERE pdg_matriculas.id_aluno = aluno.id_aluno
AND pdg_matriculas.id_serie = pdg_serie.id_serie
AND pdg_matriculas.id_turma = pdg_turma.id_turma
ORDER BY `aluno`.`nome_aluno` ASC
LIMIT 0 , 30

我尝试使用直接查询,但它无效。

$this->db->select('SELECT pdg_matriculas.id_matricula, aluno.nome_aluno, pdg_serie.nome_serie, pdg_turma.nome_turma, aluno.status_aluno, pdg_matriculas.ano_letivo
FROM pdg_matriculas, pdg_serie, pdg_turma, aluno
WHERE pdg_matriculas.id_aluno = aluno.id_aluno
AND pdg_matriculas.id_serie = pdg_serie.id_serie
AND pdg_matriculas.id_turma = pdg_turma.id_turma');
$this->db->limit(10);
$query = $this->db->get();

尝试使用Active Record但也失败了

$this->db->select('T1.id_matricula, T2.nome_aluno, T3.nome_serie, T4.nome_turma, T2.status_aluno, T1.ano_letivo',FALSE);
$this->db->from('pdg_matriculas T1, aluno T2, pdg_serie T3, pdg_turma T4');
$this->db->where('T1.id_matricula','T2.id_aluno');
$this->db->where('T1.id_serie','T3.id_serie');
$this->db->where('T1.id_turma','T4.id_turma');
$this->db->order_by('T2.nome_aluno', 'asc'); 
$this->db->limit(10);
$query = $this->db->get();

任何人都可以帮我转换为Active Record吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

试试这段代码:

 $this->db->select('T1.id_matricula, T2.nome_aluno, T3.nome_serie, T4.nome_turma, T2.status_aluno, T1.ano_letivo',FALSE);
    $this->db->from('pdg_matriculas as T1');
    $this->db->join('aluno as T2','T1.id_matricula = T2.id_aluno');   
    $this->db->join('pdg_serie as T3','T1.id_serie = T3.id_serie');
    $this->db->join('pdg_turma as T4','T1.id_turma = T4.id_turma');
    $this->db->order_by('T2.nome_aluno', 'asc'); 
    $this->db->limit(10);
    $query = $this->db->get();

答案 1 :(得分:0)

很抱歉延迟,现在它有效,当然有一些小的改动,更多的神话感谢大家并保留你的参考。

$this->db->select('T1.id_matricula,aluno.nome_aluno,pdg_serie.nome_serie,pdg_turma.nome_turma,T1.ano_letivo,T1.id_aluno as id_aluno_m,aluno.id_aluno as id_aluno_a,aluno.status_aluno');
$this->db->from('pdg_matriculas T1');
$this->db->join('aluno', 'aluno.id_aluno = T1.id_aluno');
$this->db->join('pdg_serie', 'pdg_serie.id_serie = T1.id_serie');
$this->db->join('pdg_turma', 'pdg_turma.id_turma = T1.id_turma');
$page_data ['alunos_lista'] = $this->db->get ()->result_array ();

工作完美