Execute MySQL queries to two tables at the same time and show results

0

I would like to show a query of two tables that are not related to each other. they practically contain the same fields id, name, surname, avatar, date Table "content" and table "copies", the table "copies" are actually copies of the table "content" is copied all the contento of the table "content" in the table "copies" minus the date because at the time that the "content" table is copied, the date that I add is the current date, not the one that the "content" table has already my point is that I want to show everything in html of the table "content" and the table "copies" in 2 divs in a div the table "content" and in another the table "copies" Now what I want to achieve is the following show everything in order according to your date it does not matter if they are in separate divs that at the time of showing are shown in order by their date do not show me all the divs of copies first and then all the divs of content or first all of content and then those of copies, no no no no what I want is to be shown one below the other in order of date, example a div of content and another of copies or biseverse according to its date. am I clear? Hopefully someone will help me or at least give me an idea of how I can achieve it thanks!

my CODE is as follows, but all the "content" divs are shown first and then all the "copy" divs What I want to achieve is the example I tell you about

NOTE: if you ask why there is an exact copy of the "content" table it is for certain purposes of the application that I am creating

CODE for table content:

<?php
include"conexion.php";
$query=mysqli_query($conexion,"SELECT * FROM contenido ORDER BY fecha DESC"); 


while($row=mysqli_fetch_array($query)){ ?>


  <div id="contenido">
       <table>
            <tr>
              <td>
               <p><?php echo$row["nombre"]?></p>

              </td>
              <td>
               <p><?php echo$row["apellido"]?></p>
              </td>
              <td>
               <p><?php echo$row["fecha"]?></p>
              </td>
              <div>
                <img src="avatars/<?php echo$row["avatar"]?>">

              </div>

            </tr>
       </table>

  </div>
  

CODE for table copies:

<?php

include"conexion.php";

   $query=mysqli_query($conexion,"SELECT * FROM copias ORDER BY fecha DESC"); 

    while($row=mysqli_fetch_array($query)){ ?>


      <div id="copias">
           <table>
                <tr>
                  <td>
                   <p><?php echo$row["nombre"]?></p>

                  </td>
                  <td>
                   <p><?php echo$row["apellido"]?></p>
                  </td>
                  <td>
                   <p><?php echo$row["fecha"]?></p>
                  </td>
                  <div>
                    <img src="avatars/<?php echo$row["avatar"]?>">

                  </div>

                </tr>
           </table>

      </div>



  <?php } ?>
    
asked by andy gibbs 11.05.2018 в 18:20
source

1 answer

2

You can use a union

SELECT * 
FROM (
    SELECT CAMPO1, CAMPO2, CAMPO_FECHA
    FROM TABLA1
    WHERE ...
    UNION
    SELECT CAMPO1, CAMPO2, CAMPO_FECHA
    FROM TABLA2
    WHERE ...
)
ORDER BY CAMPO_FECHA DESC

For the UNION it is important to know that both queries must have the same fields and of the same type.

    
answered by 11.05.2018 в 18:25