inner join without duplicates

0

I'm doing a system in php in which I have two tables in one save the records and in another the boxes where each record goes, the result is this way

-------- -------- --------------- 
CASILLA  SECCION  REPRESENTANTE 
-------- -------- --------------- 
B        1028     14
C1       1028     15
C2       1028

The id of the representative that I register is saved, but when the query with the inner join omits the one that does not have id, this is my query:

SELECT 
    rc.apaterno,
    rc.amaterno,
    rc.nombre,
    encarte.seccion,
    encarte.casilla
FROM
    encarte
INNER JOIN rc ON encarte.id_representante = rc.id
OR encarte.id_representante IS NULL

and bring me this data

-------- -------- ------------ ------- --------
APATERNO AMATERNO NOMBRE       SECCION CASILLA
-------- -------- ------------ ------- -------
cedillo   moller  carlos       1028    B
marco     garcia  martinez     1028    C1
    
asked by Carlos Cedillo moller 15.06.2017 в 22:46
source

2 answers

3

When you use INNER JOIN SQL brings the results where both parties have a relationship, while a LEFT OR RIGTH JOIN prioritizes the set of results that are on the indicated side, left or right, regardless of if the other side intersects. For your question your sentence would be:

SELECT 
    rc.apaterno,
    rc.amaterno,
    rc.nombre,
    encarte.seccion,
    encarte.casilla
FROM
    encarte
LEFT JOIN rc ON encarte.id_representante = rc.id

This prioritizes the table [insert] and if the table [rc] for the union (insert.id_representante = rc.id) has nothing, it will return [null]. Returning something like this to you:

NULL, NULL, NULL, 1028, B

If you want to know more about it or technically, check this link . Another thing also investigates something about alias to improve your code. Greetings!

    
answered by 15.06.2017 в 23:02
0

Your query would look like this:

SELECT rc.apaterno, rc.amaterno, rc.nombre, encarte.seccion, encarte.casilla 
FROM encarte LEFT JOIN rc ON encarte.id_representante = rc.id 
    
answered by 15.06.2017 в 22:52