query three tables with INNER JOIN

0

Hi guys, the problem I have is the following, I have three tables "post", "copies" "Friends" related to each other. I explain in the table FRIENDS the ids of the friends' sessions are saved among them, in the post table the information of those who publish a post is saved, for example their last name etc ... after that the user publishes a post there is an option or a button that when pressing the function that will do is take a copy of the desired post and save the information in the table Copies basically the same information because in reality is a copy of the post what will be saved, the only thing that will vary will be the date . Well up there all right, now what I really need is to read those two tables through a query that shows me all the contents of the POST table and COPIES in order of date I do not know if I understand, here is an example:

NOTE : this query is successful shows me all the contents of the two tables of the users that are my friends but I want to show it to me in order of date since it is showing me everything disorganized.

SELECT * FROM
         amigos a LEFT JOIN 
         contenido ON  contenido.id_user = a.para
         LEFT JOIN copias 
         ON contenido.id = copias.post_id  and copias.id_user = a.para
         WHERE  de='{$_SESSION["id"]}'



        tabla "Amigos"


        ---------|--------|---------|-----------|
         id_ami  | de     |para     |fecha      |
        ---------|--------|---------|-----------|


        tabla "Post"

        ---------|--------|---------|-----------|--------|------|
         id_post | Nombre |Apellido |Comentario |Imagen  |Fecha |
        ---------|--------|---------|-----------|--------|------|



        tabla "Copias"

        ---------|--------|---------|-----------|-----------|-------|------|
         id_cop  |id_post |Nombre   |Apellido   |Comentario |Imagen |fecha |
        ---------|--------|---------|-----------|-----------|-------|------|

I hope you can help me with how you could order it by date thanks !!

    
asked by andy gibbs 23.07.2018 в 18:31
source

2 answers

3

If I understand correctly, you want to order by always comparing two dates from two different tables that participate in JOIN .

For this, MySQL has the functions LEAST and GREATEST .

  

LEAST()

     

With two or more arguments, returns the smallest argument (minimum value). The arguments are compared using the   following rules:

     
  • If any argument is NULL , the result is NULL . No comparison is needed.

  •   
  • If all arguments have integer values, they are compared as integers.

  •   
  • If at least one argument is double-precision, they are compared as double precision values. Otherwise, if at least one argument   is a value DECIMAL , are compared as values DECIMAL .

  •   
  • If the arguments comprise a combination of numbers and strings, they are compared as numbers.

  •   
  • If any argument is a non-binary string (character), the arguments are compared as non-binary strings.

  •   
  • In all other cases, the arguments are compared as binary strings.

  •   
  • The return type of LEAST() is the aggregate type of the comparison argument types.

  •   

- LEAST() in MySQL documentation

  

GREATEST

     

With two or more arguments, returns the largest argument (maximum value). The arguments are compared using the   same rules as for LEAST() .

     

- GREATEST() in MySQL documentation

Application

Both functions can be applied to sort the data in combination with ORDER BY . Also, note that you can use several columns or values in the comparison, as we have read above.

In a proof of concept, sorting using GREATEST would be something like this:

/*Ordenar por la mayor entre las dos columnas*/
SELECT 
    *
FROM posts_20180724 p
    INNER JOIN copia_20180724 c ON p.post_id=c.post_id
ORDER BY GREATEST(p.post_date,c.copy_date);

Exit:

post_id      post_title      post_date             copy_id    copy_date
    1        Post1           01.07.2017 00:00:00    1         02.07.2017 00:00:00
    2        Post2           02.07.2017 00:00:00    2         03.07.2017 00:00:00
    4        Post4           04.07.2017 00:00:00    4         01.03.2017 00:00:00
    3        Post3           03.07.2017 00:00:00    3         04.07.2017 00:00:00

And using LEAST would be something like this:

/*Ordenar por la menor entre las dos columnas*/
SELECT 
    *
FROM posts_20180724 p
    INNER JOIN copia_20180724 c ON p.post_id=c.post_id
ORDER BY LEAST(p.post_date,c.copy_date);

Exit:

post_id     post_title      post_date             copy_id   copy_date
4           Post4           04.07.2017 00:00:00    4        01.03.2017 00:00:00
1           Post1           01.07.2017 00:00:00    1        02.07.2017 00:00:00
2           Post2           02.07.2017 00:00:00    2        03.07.2017 00:00:00
3           Post3           03.07.2017 00:00:00    3        04.07.2017 00:00:00

See Demo

Here you can see a DEMO using real data and you can do tests.

I hope it's useful.

    
answered by 24.07.2018 в 03:13
1

If I have understood correctly what you put in the comments of your question (which by the way, should be really in the question and not in the comments) 1 I propose this:

  • Create a variable of type TABLE which will have the information of the tables POST and COPIAS .
  • Modify the query you have so that it points to this variable of type TABLE to bring in order of date the posts / copies made in both tables.

This is a pseudo-code that better illustrates what is stated in the previous points:

// Esta variable de tipo TABLE guardará todos los registros de la tabla POST y COPIAS.
DECLARE @tablaResultados AS TABLE
(
     Nombre NVARCHAR(255),
     Apellido NVARCHAR(255),
     Comentario NVARCHAR(255),
     Imagen NVARCHAR(255),
     Fecha DATETIME
)

// Primero, inserto los datos de la tabla POST en la variable de tipo TABLE llamada @tablaResultados:
INSERT INTO @tablaResultado (Nombre, Apellido, Comentario, Imagen, Fecha)
SELECT Nombre, Apellido, Comentario, Imagen, Fecha
FROM POST;

// Luego inserto los datos de la tabla COPIAS en la variable de tipo TABLE llamada @tablaResultados:
INSERT INTO @tablaResultado (Nombre, Apellido, Comentario, Imagen, Fecha)
SELECT Nombre, Apellido, Comentario, Imagen, Fecha
FROM COPIAS;

// Ya luego tu consulta sería así:
// La consulta obtiene los resultados de ambas tablas "POST" y "COPIAS" ordenados por fecha (descendente) = el registro mas reciente se mostrará en primer lugar.
SELECT Nombre, Apellido, Comentario, Imagen, Fecha
FROM @tablaResultado
ORDER BY Fecha DESC;

1 The comments in the questions / answers are temporary, at any time they will be deleted.

    
answered by 23.07.2018 в 21:56