SELECT .. JOIN to relate 2 tables

1

Good evening, I have a problem because I currently have two tables in mysql and at the time of making the query I can not make them show me all their data because I do not know how to relate them. The primary keys are CedulaAlumno and CedulaRepresentante, what happens with this? that the data will never be the same and therefore I do not know how to associate them to obtain the results. Here I leave the code and the screens of the database. Thank you very much.

PS: As you can see, I do not know where I should place the inner join if I should place it.

Table students

Representatives table

Then the code that I have so far:

<!DOCTYPE html>
<html>
<head>
    <title>Consulta de Registros</title>
</head>
<body>

<?php

$Conexion = mysqli_connect("localhost", "root", "", "tesis") or die ("Problemas con la conexion");

$Registros = mysqli_query($Conexion, "select TipoCedulaAlumno, CedulaAlumno, PrimerApellidoAlumno, SegundoApellidoAlumno, PrimerNombreAlumno, SegundoNombreAlumno, FechaNacimientoAlumno, NacionalidadAlumno, EdadAlumno, SexoAlumno, DireccionAlumno, TelefonoAlumno, CorreoAlumno from alumnos where CedulaAlumno = '$_REQUEST[CedulaAlumno]'") or die ("Problemas en el select: ".mysqli_error($Conexion));

if ($Reg = mysqli_fetch_array($Registros)) 

{
    echo "Cedula del Alumno: ".$Reg['TipoCedulaAlumno']." ".$Reg['CedulaAlumno']."<br>";
    echo "Apellidos del Alumno: ".$Reg['PrimerApellidoAlumno']." ".$Reg['SegundoApellidoAlumno']."<br>";
    echo "Nombres del Alumno: ".$Reg['PrimerNombreAlumno']." ".$Reg['SegundoNombreAlumno']."<br>";
    echo "Fecha de Nacimiento del Alumno: ".$Reg['FechaNacimientoAlumno']."<br>";
    echo "Nacionalidad del Alumno: ".$Reg['NacionalidadAlumno']."<br>";
    echo "Edad del Alumno: ".$Reg['EdadAlumno']."<br>";
    echo "Sexo del Alumno: ".$Reg['SexoAlumno']."<br>";
    echo "Direccion del Alumno: ".$Reg['DireccionAlumno']."<br>";
    echo "Telefono del Alumno: ".$Reg['TelefonoAlumno']."<br>";
    echo "Correo del Alumno: ".$Reg['CorreoAlumno']."<br>";

}

else 

{
 echo "No se encontro un alumno con esa Cedula";
}
 ?>
</body>
</html>
    
asked by shadowmors 17.05.2016 в 02:05
source

2 answers

2

Assuming that the field name of the student representative of the table represented is FK, it refers to the primary key of the student table it represents.

To see all the data of the student + all the data of the representative (if there is one) you should use this SELECT:

SELECT * FROM alumnos LEFT JOIN representantes 
ON alumnos.CedulaAlumno = representantes.cedulaAlumnoRepresentante 
    
answered by 17.05.2016 в 16:40
-2

I think you are misinterpreting things, so look, what I see the 2 tables have a relationship to what is person, I mean ... are people! .. So what I would do is the following, I would create a table called PERSONA , where it would contain the simple: id_persona, Nombre, Apellido, año_nacimiento, etc.

Then I would create another table ALUMNO , where it would contain its own fields: id_alumno,fk_carrera,fk_id_persona (this is the id of the person), fk_representante (here will be a PERSON ID)

If you need more help, I can advise you via Skype: developer_01

    
answered by 17.05.2016 в 08:15