2016-03-02 46 views
0

我有一个表,我定义了一列,以接收零0值。我这样做是因为零0代表所有值。表的名字是agendas,它的列是turmas_id,这一列与表turmas有关系,但turmas_idagendas它不是一个外键,因为我可以加0前面说的那样。如何使用包含属性值零的表进行联接?

问题是,当我使用这些表进行JOIN连接时,因为我需要返回所有带有零值的属性,并在表turmas中添加了有效的键。

我试过使用LEFT JOIN和INNER JOIN,但结果并不是我在等待。如果id存在于表turmas和表agendas中,我可以使用JOIN,因为它是一个有效的外键,但我无法返回agendas属性turmas_id中的其他值,这正是我所需要的。

我该怎么做?

我需要显示此结果

//table agendas 
----------------------------------------- 
    turmas_id | descricao 
----------------------------------------- 
    0   | this attribute contain zero and it's not exists in table turmas 
    16   | table turmas contain id 16 it is a foreign key 
    0   | this attribute contain zero and it's not exists in table turmas 
    23   | table turmas contain id 23 it is a foreign key 

SQL

$agendamentos = $this->Agenda->query("SELECT * FROM responsavel_alunos RespAlunos " 
       . "INNER JOIN pessoas Responsavel ON (Responsavel.id = RespAlunos.pessoas_id) " 
       . "INNER JOIN pessoas Aluno ON (Aluno.id = RespAlunos.pessoas_id1) " 
       . "INNER JOIN matriculas Matricula ON (Matricula.pessoas_id = Aluno.id) " 
       . "RIGHT JOIN turmas Turma ON (Turma.id = Matricula.turmas_id OR Turma.id = 0) " 
       . "INNER JOIN escolas Escola ON (Escola.id = Matricula.escolas_id) " 
       . "INNER JOIN agendas Agenda ON (Agenda.turmas_id = Turma.id) " 
       . "WHERE Responsavel.id = ? ORDER BY Agenda.created DESC " 
       , array($id)); //id do responsavel 

型号

enter image description here

JSON结果

{ 
    "status": "1", 
    "result": [ 
    { 
     "RespAlunos": { 
     "id": "5", 
     "pessoas_id": "8", 
     "pessoas_id1": "9", 
     "created": "2015-09-21 10:25:46", 
     "modified": "2015-09-21 10:25:46" 
     }, 
     "Responsavel": { 
     "id": "8", 
     "nome": "responsavel ", 
     "email": "responsavel @hotmail.com", 
     "tipopessoas_id": "3", 
     "status": "1", 
     "created": "2015-09-21 10:17:17", 
     "modified": "2015-09-21 10:17:17" 
     }, 
     "Aluno": { 
     "id": "9", 
     "nome": "aluno", 
     "email": "[email protected]", 
     "tipopessoas_id": "1", 
     "status": "1", 
     "created": "2015-09-21 10:18:41", 
     "modified": "2015-09-21 10:18:41" 
     }, 
     "Matricula": { 
     "id": "6", 
     "referencia": "238", 
     "pessoas_id": "9", 
     "turmas_id": "4", 
     "escolas_id": "2", 
     "status": "1", 
     "created": "2015-09-21 10:35:08", 
     "modified": "2016-02-18 10:51:20" 
     }, 
     "Turma": { 
     "id": "4", 
     "descricao": "4º ano", 
     "created": "2015-09-21 10:31:32", 
     "modified": "2015-09-21 10:31:32" 
     }, 
     "Escola": { 
     "id": "2", 
     "descricao": "Santa Luz Unidade 2", 
     "created": "2015-09-17 23:09:38", 
     "modified": "2015-09-17 23:09:38" 
     }, 
     "Agenda": { 
     "id": "34", 
     "data": "2016-02-29 14:40:00", 
     "descricao": "<p>teste 1</p>\r\n", 
     "escolas_id": "2", 
     "turmas_id": "4", 
     "created": "2016-02-29 14:40:21", 
     "modified": "2016-02-29 14:40:21" 
     } 
    }, 
    { 
     "RespAlunos": { 
     "id": "5", 
     "pessoas_id": "8", 
     "pessoas_id1": "9", 
     "created": "2015-09-21 10:25:46", 
     "modified": "2015-09-21 10:25:46" 
     }, 
     "Responsavel": { 
     "id": "8", 
     "nome": "responsavel ", 
     "email": "responsavel @hotmail.com", 
     "tipopessoas_id": "3", 
     "status": "1", 
     "created": "2015-09-21 10:17:17", 
     "modified": "2015-09-21 10:17:17" 
     }, 
     "Aluno": { 
     "id": "9", 
     "nome": "aluno", 
     "email": "[email protected]", 
     "tipopessoas_id": "1", 
     "status": "1", 
     "created": "2015-09-21 10:18:41", 
     "modified": "2015-09-21 10:18:41" 
     }, 
     "Matricula": { 
     "id": "6", 
     "referencia": "238", 
     "pessoas_id": "9", 
     "turmas_id": "4", 
     "escolas_id": "2", 
     "status": "1", 
     "created": "2015-09-21 10:35:08", 
     "modified": "2016-02-18 10:51:20" 
     }, 
     "Turma": { 
     "id": "4", 
     "descricao": "4º ano", 
     "created": "2015-09-21 10:31:32", 
     "modified": "2015-09-21 10:31:32" 
     }, 
     "Escola": { 
     "id": "2", 
     "descricao": "Santa Luz Unidade 2", 
     "created": "2015-09-17 23:09:38", 
     "modified": "2015-09-17 23:09:38" 
     }, 
     "Agenda": { 
     "id": "27", 
     "data": "2016-02-29 08:24:00", 
     "descricao": "descricao", 
     "escolas_id": "2", 
     "turmas_id": "4", 
     "created": "2016-02-29 08:25:20", 
     "modified": "2016-02-29 08:25:20" 
     } 
    } 
    ] 
} 

回答

0

尝试在使用?而不是使用=?在你的WHERE子句中,因为你使用了一个数组变量

+0

我该怎么办? – FernandoPaiva

0

你可以试试RIGHT JOIN而不是?

"RIGHT JOIN turmas Turma ON (Turma.id = Matricula.turmas_id OR Turma.id = 0)"

因为TABLE1 LEFT JOIN TABLE2指从TABLE1所有行会被选中,即使有在TABLE2没有匹配行。

RIGHT JOIN是相反的。因此,使用RIGHT JOIN,以便您可以根据ON条件返回TURMA记录,即使上表中没有匹配的记录。

+0

我编辑了帖子并添加了JSON结果。看看结果'议程'它不包含'turmas_id'中0的所有结果 – FernandoPaiva

0

试试这个。

$agendamentos = $this->Agenda->query("SELECT * FROM responsavel_alunos RespAlunos " 
       . "INNER JOIN pessoas Responsavel ON (Responsavel.id = RespAlunos.pessoas_id) " 
       . "INNER JOIN pessoas Aluno ON (Aluno.id = RespAlunos.pessoas_id1) " 
       . "INNER JOIN matriculas Matricula ON (Matricula.pessoas_id = Aluno.id) " 
       . "(SELECT turmas as turma WHERE (turma.id = Matricula.turmas_id OR turma.id = 0)) AS TURMA" 
       . "INNER JOIN escolas Escola ON (Escola.id = Matricula.escolas_id) " 
       . "INNER JOIN agendas Agenda ON (Agenda.turmas_id = Turma.id) " 
       . "WHERE Responsavel.id IN ? ORDER BY Agenda.created DESC " 
       , array($id)); //id do responsavel 
+0

我试过了你的建议,但抛出了SQL语法的异常。你可以帮我吗 ? – FernandoPaiva

相关问题