1 Duplicate results with INNER JOIN

0

Thanks for letting me register here.

In this opportunity I am a programmer of a new social network which took a day with only one problem. I have 3 tables and of the 3 tables one is duplicating me the images or tripling.

<?php

            $query="SELECT 
                      clit.id_cliente,
                      clit.pais,
                      clit.nom_cli,
                      Vis.id_cliente,
                      Vis.ID,
                      Vis.video,
                      Vis.date,
                      fulls.id_cliente,
                      fulls.ID,
                      fulls.name,
                      fulls.foto 
              FROM cliente clit 
                      INNER JOIN videos Vis  ON clit.id_cliente = Vis.id_cliente
                      INNER JOIN fullsuave fulls ON clit.id_cliente = fulls.id_cliente
                      AND clit.id_cliente = clit.id_cliente
              ORDER BY clit.id_cliente, clit.id_cliente      
                        LIMIT 0, 25; ";  

?>

Why would the duplication please help me?

    
asked by Maria Alejandra Cordero 26.07.2018 в 02:14
source

2 answers

-1

By reproducing your model, after having tried by all means to indicate the structure of your tables, you can verify that if the data is good there is no problem in obtaining the expected results.

For example, in a design where client 1 has two videos, the following query:

SELECT 
    c.cliente_id, 
    c.cliente_nom,
    v.video,
    f.foto
FROM cliente_20180725 c
INNER JOIN video_20180725 v ON c.cliente_id=v.cliente_id 
INNER JOIN full_20180725  f ON c.cliente_id=f.cliente_id ;

Throw the following result:

cliente_id  cliente_nom      video                      foto
-------------------------------------------------------------------
1            Cliente 1        Vídeo 1 Cliente 1         foto1.jpg
1            Cliente 1        Vídeo 2 Cliente 1         foto1.jpg
2            Cliente 2        Vídeo 1 Cliente 2         foto2.jpg
3            Cliente 3        Vídeo 1 Cliente 3         foto3.jpg

Neither do you know exactly what you want. If for example you need to group the videos by client, etc.

All this can be done with relative ease, as long as you are clear in explaining the expected results.

Applying a certain order is also easy.

Test data

These are the data that I used. It is a minimum example, it does not have restrictions of uniqueness or established indexes.

If I have not understood the data model well or if there is something that is not clear, you can indicate it in a comment.

I hope it is useful and helps to clarify or solve the problem.

SEE DEMONSTRATION IN REXTESTER

CREATE TABLE IF NOT EXISTS cliente_20180725 
(
    cliente_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    cliente_nom VARCHAR(50)
)ENGINE=INNODB;

INSERT INTO cliente_20180725 (cliente_nom) VALUES 
    ('Cliente 1'),
    ('Cliente 2'),
    ('Cliente 3'),
    ('Cliente 4')
    ;


CREATE TABLE IF NOT EXISTS video_20180725 
(
    video_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    cliente_id   INT,
    video VARCHAR(50),
    FOREIGN KEY (cliente_id) REFERENCES cliente_20180725(cliente_id) 
    ON UPDATE CASCADE ON DELETE  CASCADE
)ENGINE=INNODB;

INSERT INTO video_20180725 (cliente_id,video) VALUES 
    (1,'Vídeo 1 Cliente 1'),
    (1,'Vídeo 2 Cliente 1'),
    (2,'Vídeo 1 Cliente 2'),
    (3,'Vídeo 1 Cliente 3')
    ;

CREATE TABLE IF NOT EXISTS full_20180725 
(
    full_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    cliente_id   INT,
    foto VARCHAR(50),
    FOREIGN KEY (cliente_id) REFERENCES cliente_20180725(cliente_id) 
    ON UPDATE CASCADE ON DELETE  CASCADE
)ENGINE=INNODB;

INSERT INTO full_20180725 (cliente_id,foto) VALUES 
    (1,'foto1.jpg'),
    (2,'foto2.jpg'),
    (3,'foto3.jpg'),
    (4,'foto4.jpg')
    ;

SELECT 
    c.cliente_id, 
    c.cliente_nom,
    v.video,
    f.foto
FROM cliente_20180725 c
INNER JOIN video_20180725 v ON c.cliente_id=v.cliente_id 
INNER JOIN full_20180725  f ON c.cliente_id=f.cliente_id 
ORDER BY c.cliente_id LIMIT 0,25;

EDITION

If you want to group by client, you can write the query like this:

SELECT 
    c.cliente_id, 
    c.cliente_nom,
    GROUP_CONCAT(v.video SEPARATOR '|')  videos,
    f.foto
FROM cliente_20180725 c
INNER JOIN video_20180725 v ON c.cliente_id=v.cliente_id 
INNER JOIN full_20180725  f ON c.cliente_id=f.cliente_id 
GROUP BY c.cliente_id,f.foto ORDER BY c.cliente_id 
LIMIT 0,25;

This query will bring you one row for each client and the videos grouped by a | separator, which you can later break down by programming.

cliente_id       cliente_nom     videos                                     foto
----------------------------------------------------------------------------------------------
1                Cliente 1       Vídeo 2 Cliente 1|Vídeo 1 Cliente 1        foto1.jpg
2                Cliente 2       Vídeo 1 Cliente 2                          foto2.jpg
3                Cliente 3       Vídeo 1 Cliente 3                          foto3.jpg
    
answered by 26.07.2018 / 03:53
source
0

from what I see in your second join you have

AND clit.id_client = clit.id_client

that is referring to the client table itself, that is wrong, but to know the complete problem it would be good to know the structures of your tables

    
answered by 26.07.2018 в 03:11