将此包含由相同列名相关的逗号连接的 SQL 转换为 CodeIgniter 活动记录脚本

问题描述 投票:0回答:3

这是我需要转换的MySQL查询:

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();
php codeigniter join activerecord query-builder
3个回答
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();

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

工作完美


0
投票

不再推荐老式的逗号连接,因为开发人员在查看 SQL 时很容易忽视它们。 相反,构建一个显式使用关键字 JOIN 的查询,并将该 JOIN 的条件附加在同一行中。

因为 JOINed 表之间的相关列是相同的,所以您可以享受

USING()
的甜蜜语法糖,以实现 SQL 简洁。

limit()
from()
调用可以合并到
get()
调用(无论如何你都必须进行。

$this->db
    ->select([
        'pm.id_matricula',
        'a.nome_aluno',
        'ps.nome_serie',
        'pt.nome_turma',
        'pm.ano_letivo',
        'pm.id_aluno  id_aluno_m',
        'a.id_aluno  id_aluno_a',
        'a.status_aluno'
    ])
    ->from('pdg_matriculas pm')
    ->join('aluno a', 'id_aluno')
    ->join('pdg_serie ps', 'id_serie')
    ->join('pdg_turma pt', 'id_turma')
    ->order_by('pm.nome_aluno', 'ASC')
    ->get('pdg_matriculas pm', 10)
    ->result_array();

呈现有效的 SQL:

SELECT `pm`.`id_matricula`, `a`.`nome_aluno`, `ps`.`nome_serie`, `pt`.`nome_turma`, `pm`.`ano_letivo`, `pm`.`id_aluno` `id_aluno_m`, `a`.`id_aluno` `id_aluno_a`, `a`.`status_aluno`
FROM `pdg_matriculas` `pm`
JOIN `aluno` `a` USING (`id_aluno`)
JOIN `pdg_serie` `ps` USING (`id_serie`)
JOIN `pdg_turma` `pt` USING (`id_turma`)
ORDER BY `pm`.`nome_aluno` ASC
LIMIT 10
© www.soinside.com 2019 - 2024. All rights reserved.