Make INNER JOIN of a foreign key

1

How do I see the description of the department using an SQL query that calls all the people's records?

This is the query I have

SELECT p.nombre, p.apellido1, p.apellido2, p.dep 
FROM personas p, departamentos d 
WHERE p.dep = d.dep
    
asked by Adolfi Téllez 19.12.2018 в 20:35
source

3 answers

2

You must do it through the use of JOIN since you are trying to link 2 tables; one of them by its PK that is dep and another one by its FK in people that is dep

SELECT departamentos.departamento, personas.nombre, personas.apellido1
FROM departamentos
INNER JOIN personas ON departamentos.dep = personas.dep;

The previous query will look for the coincidence in both tables and will show you the columns that you specify explicitly require

THE USE OF INNER JOIN

  

The union and subsequent results that you return to us will be   conditioned to that much in the table on the left that   is (departments), as in the one on the right that is (people) exist   linked records, omitting those records that only exist in   one of the 2 tables but not both

    
answered by 19.12.2018 в 20:42
1

You say have the following query

SELECT p.nombre, p.apellido1, p.apellido2, p.dep FROM personas p, departamentos d WHERE p.dep = d.dep

I think that to link your table what it requires is a join so that you can access from your query to the attributes of your table

you could do something like this

SELECT p.nombre, p.apellido1, p.apellido2, p.dep, d.ped as relacion, d.descripcion 
FROM personas p
inner join departamento d on d.ped = p.dep
WHERE p.dep = d.dep
    
answered by 19.12.2018 в 20:49
0

I've got it this way:

SELECT p.nombre, p.apellido1, p.apellido2, *d.departamento*'
FROM personas p, departamentos d
WHERE p.dep = d.dep

I just call the department's description.

I do not know if this is considered JOIN , but it works perfectly for me. Thanks to everyone.

    
answered by 19.12.2018 в 20:57