简体   繁体   中英

How do I get last record from a mysql column through php where several other query is running

Im trying to get several user data (from a Voip Calling Card Database) to a web portal from multiple table where Login , Name , Registration Date taken from one table, Last Successful Call Date is in another table and Current User balance and Total Duration is in another table. Here is the demo screencap:

在此输入图像描述

My problem is im not able to get the last call date which have to query and get the last or latest date from a list of total call history by filtering each user individually.

Codes are given below:

All of the needed data is in these 3 table "clientsshared, invoiceclients, calls,"

"clientsshared" holds Login & balance data.

"invoiceclients" holds Name and Account Creation Date.

"calls" holds call duration and all other call history

<div>
  <table class="table table-striped table-bordered" cellspacing="0" width="100%">
    <thead>
      <tr>
        <th>#</th>
        <th>Login</th>
        <th>Full Name</th>
        <th>Reg.Date</th>
        <th>LastCall</th>
        <th>Current Balance</th>
        <th>Total Duration</th>
      </tr>
    </thead>
    <tbody>
      <?php 
         $sql="select c.login,cname.Name,cname.LastName,DATE_FORMAT(cname.Creation_Date,'%d-%m-%y')as regdate,cdr.call_start,c.account_state,sum(cdr.duration / 60) as total_duration from clientsshared as c
         left join invoiceclients as cname on cname.IdClient = c.id_client
         left join calls as cdr on cdr.id_client = c.id_client
         where c.id_reseller='10' group by c.id_client order by total_duration desc limit 100" ; 

        $result=$ db1->query($sql); 

        if($result){ 
        $i = 1; 
        while($row = $result->fetch_object()){ 
            $login = $row->login; 
            $Name = $row->Name; 
            $LastName = $row->LastName; 
            $RegDate = $row->regdate; 
            $LastCall = $row->call_start; 
            $account_state = $row->account_state; 
            $total_duration = $row->total_duration; 
       ?>

      <tr>
        <td><?php echo $i;?></td>
        <td><?php echo $login; ?></td>
        <td><?php echo $Name. " ".$LastName; ?></td>
        <td><?php echo $RegDate; ?></td>
        <td><?php echo $LastCall; ?></td>
        <td><?php echo round($account_state,2); ?></td>
        <td><?php echo round($total_duration,2); ?></td>
      </tr>

      <?php 
        $i++; 
        } 
       } 
      ?>

    </tbody>
  </table>
</div>

==== Updated Problem and Solved======

There is a new issue im facing, I've tried to add a new table by Left Join which is a payment history table but after joining this table the actual total Duration field giving wrong values

New query is here:

 $sql = "select c.login,cname.Name,cname.LastName,DATE_FORMAT(Creation_Date,'%m-%d-%y')as regdate, (Select max(data) from payments where payments.id_client = c.id_client) as lastpayment, (Select max(call_start) from calls where calls.id_client = c.id_client) as lastcall, c.account_state,sum(cdr.duration / 60) as total_duration from clientsshared as c left join invoiceclients as cname on cname.IdClient = c.id_client left join payments as p on p.id_client = c.id_client left join calls as cdr on cdr.id_client = c.id_client where c.id_reseller='10' group by c.id_client order by total_duration desc limit 100"; 
在此输入图像描述



Solved wow, Im not sure how it works but I just removed a left join and tried which output the correct value as expected ,

  select c.login,cname.Name,cname.LastName,cname.Creation_Date as regdate, (Select max(data) from payments where payments.id_client = c.id_client) as lastpayment, (Select max(call_start) from calls where calls.id_client = c.id_client) as lastcall, c.account_state,sum(cdr.duration / 60) as total_duration from clientsshared as c left join invoiceclients as cname on cname.IdClient = c.id_client left join calls as cdr on cdr.id_client = c.id_client where c.id_reseller='10' group by c.id_client order by total_duration desc 

Posting it as an answer as it is too long:

As a said, I do not see the field for last call date in your select statement of $sql. If you have a direct column for last call date in Calls table then include it select statement. So your query should look something like this:

    select c.login,cname.Name,cname.LastName,DATE_FORMAT(Creation_Date,'%d-%m-%y')as regdate,(Select max(call_start) from calls where calls.id_client = c.id_client) as lastcall,
 c.account_state,sum(cdr.duration / 60) as total_duration from clientsshared as c
         left join invoiceclients as cname on cname.IdClient = c.id_client
         left join calls as cdr on cdr.id_client = c.id_client
         where c.id_reseller='10' group by c.id_client order by total_duration desc limit 100"

I have added (Select max(cdr.call_start) from calls where calls.id_client = c.id_client) in your query.

Finally its solved, Thank you all.

Resolved query:

 $sql = "select c.login,cname.Name,cname.LastName,DATE_FORMAT(Creation_Date,'%d-%m-%y')as regdate,DATE_FORMAT((Select max(call_start) from calls where calls.id_client = c.id_client),'%d-%m-%y') as lastcall, c.account_state,sum(cdr.duration / 60) as total_duration from clientsshared as c left join invoiceclients as cname on cname.IdClient = c.id_client left join calls as cdr on cdr.id_client = c.id_client where c.id_reseller='10' group by c.id_client order by total_duration desc limit 100"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM