簡體   English   中英

如何從運行其他幾個查詢的php獲取mysql列的最后一條記錄

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

我試圖從多個表中獲取多個用戶數據(從Voip電話卡數據庫)到門戶網站,其中登錄名稱注冊日期取自一個表, 上次成功通話日期在另一個表中,當前用戶余額和總持續時間是在另一張桌子里。 這是演示screencap:

在此輸入圖像描述

我的問題是我無法通過單獨過濾每個用戶來獲取必須查詢並從總呼叫歷史列表中獲取最后或最后日期的最后一個呼叫日期。

代碼如下:

所有需要的數據都在這3個表中“clientsshared,invoiceclients,calls”

“clientsshared”保存登錄和平衡數據。

“invoiceclients”持有名稱和帳戶創建日期。

“呼叫”保持呼叫持續時間和所有其他呼叫歷史記錄

<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>

====更新問題並解決了======

我面臨一個新問題,我試圖通過Left Join添加一個新表,這是一個付款歷史表,但在加入此表后,實際的總持續時間字段給出了錯誤的值

新查詢在這里:

 $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"; 
在此輸入圖像描述



解決了哇,我不知道它是如何工作的但我只是刪除了一個left join並嘗試輸出正確的值,如預期的那樣,

  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 

將其作為答案發布,因為它太長了:

如上所述,我沒有在$ sql的select語句中看到最后一個調用日期的字段。 如果在Calls表中有最后一個呼叫日期的直接列,則將其包含在select語句中。 所以你的查詢應該是這樣的:

    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"

我在查詢中添加了(從調用中選擇max(cdr.call_start)來調用call.id_client = c.id_client)

最后它解決了,謝謝大家。

已解決的查詢:

 $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"; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM