2016-03-02 43 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

相關問題