how to do an INNER JOIN and show in php

1
  

Table notes with the following fields

The problem is that the student ID comes out, and I need to bring the student's name from the table alumnos

I currently have my query on php in the following way:

    $getNotas = mysqli_query($con,"SELECT * FROM notas WHERE grupo='$grupo'") or die ('error al obtener datos de notas');

How can I bring the names of the students from the other table.

I have tried to use more I do not know if it is well structured

$getNotas = mysqli_query($con,"SELECT
        nt.ID NOTA_ID ,
        nt.grupo grupo,
        nt.unidad_1 unidad1,
        nt.unidad_2 unidad2,
        nt.unidad_3 unidad3,
        nt.unidad_4 unidad4,
        al.ID ALUMNO_ID,
        al.nombre nombrealumno, FROM notas nt INNER JOIN alumnos al ON nt.NOTA_ID = al.ALUMNO_ID WHERE nt.grupo='$grupo'") or die ('error al obtener datos de notas'.mysqli_connect_error());

Send the message "error when obtaining note data". It does not have syntax errors, but it does not show me anything.

    
asked by DoubleM 29.11.2017 в 04:51
source

1 answer

1

At first glance, your query is well constructed. The main detail seems to be with your join condition:

ON nt.ID = al.id_alumno

It is necessary that you make the join with the columns of both tables that correspond to the student ID. nt.ID does not seem to be that, rather it seems to be the ID of the note.

I'm not clear about the names of the columns in your notas table, but the condition should be something like this:

ON nt.alumno = al.id_alumno

... where nt.alumno corresponds to the column in the notas table that contains the student ID. Adjust the name of the column if necessary.

Edit

Well, maybe there are other small adjustments that you will need to make in addition to the above:

  • nt.nombre alumno, : It does not seem correct that nt.nombre corresponds to the name of the student. Unless this represents some other necessary data, remove it from the query. The student's name is already retrieved with al.nombre nombrealumno .
  • nt.ID ID and al.id_alumno ID : You are assigning the alias ID to 2 columns with different meanings. To avoid problems, allocate different aliases. Example: nt.ID NOTA_ID and al.id_alumno ALUMNO_ID .
  • al.unidad4 unidad_4 : This is an error. It should be: nt.unidad4 unidad_4 .
  • answered by 29.11.2017 / 05:37
    source