How to join tables in MySQL?

0

I have three related tables in this way:

What I want to do is make the final result look like this:

The SQL query I did was the following:

select pe.id_pago_empleado,e.nombre,e.apellido_paterno,e.apellido_materno,pe.sueldo,pe.frecuencia_pago,p.puesto
from empleado as e inner join pago_empleado as pe on e.id_empleado = pe.id_empleado
inner join puesto_empleado as p on p.id_puesto_empleado = e.id_puesto 
where e.estado = 'Activo' order by e.nombre asc;

Even having records in the database that meet that condition does not throw data into the query. But if I only relate two tables for example empleado and pago_empleado or empleado and puesto_empleado if you show me records. I'm really new in this I hope you can help me thanks.

    
asked by U.C 29.07.2017 в 21:04
source

3 answers

0

Good morning.

I tell you that personally I leave the table that returns the desired universe of data; for your case (I assume) is employee ; that is:

 /* El "as" no es obligatorio para asignar el alias */
SELECT EP.ID_EMPLEADO, EP.NOMBRE, EP.APELLIDO_PATERNO, EP.APELLIDO_MATERNO FROM EMPLEADO EP;

Now we require the position also; it would be:

/* En alias para los campos si uso el "as" :D */
SELECT EP.ID_EMPLEADO, EP.NOMBRE, EP.APELLIDO_PATERNO, EP.APELLIDO_MATERNO, 
PE.NOMBRE AS PUESTO
FROM EMPLEADO EP
INNER JOIN PUESTO_EMPLEADO PE ON PE.id_puesto_empleado = EP.id_puesto;

Now you also require payment and frequency ; it would be:

SELECT EP.ID_EMPLEADO, EP.NOMBRE, EP.APELLIDO_PATERNO, EP.APELLIDO_MATERNO, 
PE.NOMBRE AS PUESTO, PAG.SUELDO, PAG.FRECUENCIA_PAGO
FROM EMPLEADO EP
INNER JOIN PUESTO_EMPLEADO PE ON PE.id_puesto_empleado = EP.id_puesto;
INNER JOIN PAGO_EMPLEADO PAG ON PAG.ID_EMPLEADO=EP.ID_EMPLEADO
ORDER BY EP.NOMBRE ASC;

According to the data you share in the illustration of your table scheme and what you want to obtain is what I suggest; considers that the INNER JOIN eliminates the EMPLOYEE records from the result of the query if there is no data recorded in the other tables or at least in one of them (those that are involved) ).

I like to start from the general to the particular with new things, and with practice in mind it is clear what to do with what you see and know should be the result.

Of course you add the WHERE as appropriate and verifying that it is true; see if "active" , "Active" , [...] and "ACTIVE" are the same when comparing in the database

May it be useful to you.

    
answered by 29.07.2017 / 21:30
source
0

Initially an error that I see is that in the WHERE condition it is looking for records where the state is active, but in its used table I do not see that it has the status field.

    
answered by 29.07.2017 в 21:12
0

you do not have the following columns in the select: pe.id_employee, e.id_puesto, e.estado, but if you have them in the join, try to do it like this:

select pe.id_pago_empleado,e.nombre,e.apellido_paterno,e.apellido_materno,pe.sueldo,pe.frecuencia_pago,p.puesto,pe.id_empleado,e.id_puesto,e.estado
from empleado as e inner join pago_empleado as pe on e.id_empleado = pe.id_empleado
inner join puesto_empleado as p on p.id_puesto_empleado = e.id_puesto 
where e.estado = 'Activo' order by e.nombre asc;
    
answered by 29.07.2017 в 21:34