mysql Queries INNER JOIN Doubt

0

I have 3 tables 2 of them will show the main content of the web my query is not giving me the expected results since I just started to use inner join for performance reasons in the consultations and to facilitate more the code of course, in order definition of the 3 tables: table "FRIENDS" contains the friends among themselves, table "CONTENT" contains the post with photo, name, surname etc .. and the other is table is "COPIES" is practically a copy of the post bone of the table "CONTENT" I need to show the content of those 2 tables sorted by date but with some conditions such as that shows me the post of only my friends and the copies that there are of those post of the table COPIES that it sounds strange what of the copies but they are my ideas hehe here I share my code and image of the related tables.

NOTE: what I want to achieve I have already achieved but not with inner join if not with separate queries but I want my code to be efficient I do not know if I understand, this is my code.

As you will see the tables are related including the main one that is the record and you can see that content and copy are related by the id of the table contained with the id_post of the table copies, so every time the user makes a copy of his post that id of the post is saved the table copies as id_post

 <?php
        /*aqui voy a sacar el id de las personas que son mis amigos, nota:
     el campo *DE* contiene mi id de session y el campo *PARA* contiene los id
    de los usuarios que yo le envie la solicitud ojala entiendan */
     $get_my_freinds_id=mysqli_query($conexion,"SELECT * FROM amigos 
          WHERE  de='{$_SESSION["id"]}' ");
          $row=mysqli_fetch_array( $get_my_freinds_id);
           $myselect =mysqli_query($conexion," SELECT * FROM
         contenido INNER JOIN copias 
         ON contenido.id = copias.post_id 
         WHERE contenido.id_user='{$row["para"]}' OR 
          copias.id_user='{$row["para"]}'");

             while($row2=mysqli_fetch_array($myselect)){

             ?>
  <!-- aqui en un div quiero mostrar la informacion de las dos tablas pero estoy un poco perdido con respecto a estas consultas -->
             <div>
              <img src="img/<?php echo$row2["avatar"]?>         
              <a href="#"><?php echo $row2["usuario"]?></a>
            </div>
         <?php  } ?>

As you will see I want to show both the post of the table content and the post of the table copies together, of the users that are my friends any help or tips I would like much friends or believe that if I get it right with separate queries I could keep doing that regardless of the performance?

    
asked by andy gibbs 07.06.2018 в 05:45
source

1 answer

0
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"]}'

You had it done, it's just joining the two queries. and in ONs put all the conditions you want.

    
answered by 07.06.2018 в 07:23