SQL Query Simple

Asked

Viewed 87 times

3

I’m starting with Database, and I came across a situation that I just don’t 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 didn’t work

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

I have 3 tables.

Table Tbaluno:

|---TbAluno---|

 *ID

 *NomeAluno

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

Table Tbmaterias:

|---TbMaterias ---|

*ID

*NomeMateria

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

and the Tbmateriasstudent table

|---TbMateriasAluno---|

 *ID

 *ID_Aluno

 *ID_Materia

|--------------
  • Look almost TbAluno.NomeAluo for Tbalu.Nomeluno and that worked? and see tutorial more easily http://www.devmedia.com.br/utilizando-joins-em-sql/1071

  • Kingrider did not work not I until I can catch one or the other, I can get the name of the student but I can not get the name of the matter. Could you pass me this query for me to study?

  • Um, here ran straight and did not understand your error, what if using oracle? mssql? mysql? this depends on the language to work sql differentiated, and but see the posted Allan is found little difference. It worked?

2 answers

2


According to your data model, I understand that you should Join between 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 it is necessary 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 )
  • Thank you Eric Roberto That was exactly the mistake, If it is not ask much what the meaning of these parentheses can not see how they are being used. Even so Thank you.

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

Behold this example working in Sqlfiddle.

  • Thank you Allan By Example, è As I said above, it is called syntax error must be my bank " Access " But vlws same

Browser other questions tagged

You are not signed in. Login or sign up in order to post.