PHP MySql打印数据表按日期排序

I have 3 tables

table 1 : tbl_marketing , table 2 : tbl_phonecall , table 3 :tbl_email

table 2 and table 3 contain the id of table 1

table 2:

enter image description here

table 3:

enter image description here

Now i want to print an output tables for user

The output will be 2 tables one contain the data of table 2 and the second the data of table 3 where the two tables contain the same id of table 1 and if you check above the id of table 1 will be stored in db_mid in table 2 and table 3

and that it's not my problem my problem is i want to print table before another table

table 2 and table 3 can contain main rows that can have same id of table 1 and i want to show them all

if db_phdate in table 2 is bigger than db_edate in table 3 table 2 will be printed first than table 3 will be printed

if not if db_edate in table 3 bigger than db_phdate in table 2 table 3 will be printed first than table 2 will be printed

How can i do that inside a while loop that fetch data from mysql database

PHP Code

Fetch Data from Database

$query=mysqli_query($conn,"Select * from tbl_phonecall,tbl_email 
    where tbl_phonecall.db_mid='$getid' or tbl_email.db_mid='$getid'")or die(mysqli_error($conn));
    while($row=mysqli_fetch_array($query)){
    $phid=$row['db_id'];
    $phdate=$row['db_phdate'];
    $phsubject=$row['db_subject'];
    $phdesc=$row['db_desc'];
    $phdue=$row['db_due'];
    $phnextdate=$row['db_nextdate'];
    $phduration=$row['db_duration'];
    $phclient=$row['db_client'];
    $phstatus=$row['db_phstatus'];
    $phnote=$row['db_note'];
    $phuser=$row['db_phuser'];
    $phisdone=$row['db_isdone'];
    $phlogs=$row['db_logs'];
    $phcrf=$row['db_crf'];
    $phphone=$row['db_phone'];
    $phcallto=$row['db_callto'];
    /***************************/
    $eid=$row['db_eid'];
    $edate=$row['db_edate'];
    $esubject=$row['db_esubject'];
    $edesc=$row['db_edesc'];
    $edos=$row['db_dos'];
    $edor=$row['db_dor'];
    $eclient=$row['db_eclient'];
    $estatus=$row['db_estatus'];
    $enote=str_replace('
','<br>',$row['db_note']);
    $euser=$row['db_euser'];
    $elogs=$row['db_logs'];
    $eerf=$row['db_erf'];
    $eemail=$row['db_email'];
    $eemailto=$row['db_emailto'];
}

Print html head of table 2

 echo'<div class="table-responsive">';
    echo"<table class='ol-md-12 table-bordered table-striped table-condensed cf table-bordered' >";
    echo"<thead class='cf'>";
    echo"<tr>";
echo"
<th >#</th>
<th >Date</th>
<th >Customer</th>
<th >Subject</th>
<th >Desc</th>
<th >Staff</th>
<th >Due</th>
<th >Next Date</th>
<th >Duration</th>
<th >Status</th>
<th >Client</th>
<th >call received from</th>
<th >Callto</th>
<th >Phone</th>
<th >IsDone</th>
<th >Logs</th>
<th >Note</th>
<th >RM</th>
<th >Edit</th>";

Html head table 3

<th >#</th>
<th >Date</th>
<th >Subject</th>
<th >Staff</th>
<th >Desc</th>
<th >Client</th>
<th>Email Receive From</th>
<th >Email To</th>
<th >Email</th>
<th >Date Of Sending</th>
<th >Date Of Receiving</th>
<th >Status</th>
<th >Logs</th>
<th>Note</th>
<th >RM</th>
<th >Edit</th>";