SQL Simple Query

3

I'm starting with Database, and I came across a situation that I just do not understand.

I wanted to get the name of the student and the name of the subject that are in table 3 in the case " TbMateriasAluno "

I used this Select command but it did not work

Select TbAluno.NomeAluno , TbMaterias from TbAluno Inner Join 
 TbMateriasAluno On TbMateriasAluno.ID_Aluno = TbAluno.NomeAluo

I have 3 Tables.

TbAluno Table:

|---TbAluno---|

 *ID

 *NomeAluno

|--------------

Table TbMaterials:

|---TbMaterias ---|

*ID

*NomeMateria

|--------------

and the TbMaterialsAlumn table

|---TbMateriasAluno---|

 *ID

 *ID_Aluno

 *ID_Materia

|--------------
    
asked by anonymous 19.08.2016 / 16:14

2 answers

2

According to your data model, I understand that you should join the 3 tables to get the desired result:

Select 
   NomeAluno, TbMaterias.NomeMateria
From 
   TbAluno
   INNER Join TbMateriasAluno On ( TbAluno.ID = TbMateriasAluno.ID_Aluno )
   Inner Join TbMaterias On ( TbMateriasAluno.ID_Materia = TbMaterias.ID )

Query adapted to run in Access Note that you need to include parentheses, so that this database understands the relationship between the tables:

Select 
   NomeAluno, TbMaterias.NomeMateria
From 
   ( TbAluno
   INNER Join TbMateriasAluno On  TbAluno.ID = TbMateriasAluno.ID_Aluno  )
   Inner Join TbMaterias On ( TbMateriasAluno.ID_Materia = TbMaterias.ID )
    
19.08.2016 / 17:38
1

You can use the following query :

select 
  a.NomeAluno
  , m.NomeMateria
from TbAluno as a
left join TbMateriasAluno as ma on ma.ID_Aluno = a.id
left join TbMaterias as m on m.id = ma.ID_Materia

See this working example in SQLFiddle .

    
19.08.2016 / 16:39