I have 2 tables, 1 of students and another called student_registration
I need to make a select from the students table, get their id and with that id enter the student_matricula table and show the enrollment number
the students table is like this:
id string,
nombres string,
direccion string,
edad int
and the student_registration table is like this
alumno_id,
numero_matricula,
fecha_matricula
I need to show the name of the student and his registration number
for instance
David 2832-1234
I tried to do it with a select from join but it doesn't work this way
SELECT e.nombres, e.id
FROM alumnos e JOIN alumno_matricula e2 SELECT e2.numero_matricula WHERE e.id = e2.alumno_id
hi can you use this
With this you match the id and you can access the value of the enrollment, in case the student does not have enrollment you could use something like this to show the value in NULL
And if you wanted to show something specific instead of null it could be
Use one
INNER JOIN
like this:Explanation
SELECT
to get the data, within it you can only do it by writingtablaNombre1.columna
,tablaNombre2.columna
INNER JOIN
or justJOIN
what it does is get the data from both tables, where the record on the left (id) matches the record on the right (student_id) and discarding the othersI notice in your comments that you also want those that do not have tuition; then you must use
LEFT JOIN
so that the result considers both those records that coincide in both tables and those records on the left side (students) that have no link with the table on the right (matricula_alumnos)Reference sources
JOIN
SyntaxYou have to indicate what type of JOIN it is, there are 3 types,
LEFT JOIN
,INNER JOIN
,RIGHT JOIN
to understand each type I recommend you search for information about each one on the internet, An example . In this case it is requiredINNER JOIN
, the JOIN condition must be indicated withON
, and the select of both tables goes in a singleSELECT
, not in two, so your query would beNormally, JOINs are a bit complicated for me, that's why I usually use plain instructions... Something like this should work 100%