[英]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. 我试图从多个表中获取多个用户数据(从Voip电话卡数据库)到门户网站,其中登录 , 名称 , 注册日期取自一个表, 上次成功通话日期在另一个表中,当前用户余额和总持续时间是在另一张桌子里。 Here is the demo screencap: 这是演示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," 所有需要的数据都在这3个表中“clientsshared,invoiceclients,calls”
"clientsshared" holds Login & balance data. “clientsshared”保存登录和平衡数据。
"invoiceclients" holds Name and Account Creation Date. “invoiceclients”持有名称和帐户创建日期。
"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 我面临一个新问题,我试图通过Left Join添加一个新表,这是一个付款历史表,但在加入此表后,实际的总持续时间字段给出了错误的值
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 , 解决了哇,我不知道它是如何工作的但我只是删除了一个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
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. 如上所述,我没有在$ sql的select语句中看到最后一个调用日期的字段。 If you have a direct column for last call date in Calls table then include it select statement. 如果在Calls表中有最后一个呼叫日期的直接列,则将其包含在select语句中。 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. 我在查询中添加了(从调用中选择max(cdr.call_start)来调用call.id_client = c.id_client) 。
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";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.