php - 如何通过运行其他几个查询的 php 从 mysql 列中获取最后一条记录

标签 php mysql

我试图从多个表中获取多个用户数据(从 Voip 电话卡数据库)到门户网站,其中 LoginNameRegistration Date 取自一个表,上次成功通话 日期在另一个表中,当前用户余额和总持续时间在另一个表中。这是演示屏幕截图:

enter image description here

我的问题是我无法获取最后一次通话日期,必须通过单独过滤每个用户来查询并从总通话记录列表中获取最后或最新日期。

代码如下:

所有需要的数据都在这 3 个表“clientsshared、invoiceclients、calls”中

“clientsshared”保存登录和余额数据。

“invoiceclients”保存姓名和帐户创建日期。

“calls”保存通话时长和所有其他通话历史

<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";
enter image description here



已解决 哇,我不确定它是如何工作的,但我只是删除了一个 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 的选择语句中没有看到最后调用日期字段。如果您在 Calls 表中有一个直接列用于上次调用日期,则将其包含在选择语句中。所以你的查询应该是这个样子:

    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"

我在您的查询中添加了(Select max(cdr.call_start) from calls where calls.id_client = c.id_client)

关于php - 如何通过运行其他几个查询的 php 从 mysql 列中获取最后一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31780473/

相关文章:

php - 将斜杠 "/"添加到 URL

python - 通过从 SQL DB 导入的 pd 数据帧的子集提高循环性能

php - 修改我的 php-mysql 机制以在结果中包含自定义多个复选框值

php - 检查更新是否确实更改了任何单元格(mysql、php)

php - 使用 require_once ('filename.php' ) 还是 require_once 'filename.php' 更好?

php在数据库中创建具有未知数量列的html表

mysql - 当我尝试创建表时在 mysql 上出现错误 1064 <42000>

php - PhpStorm 中 Xdebug 的条件断点

Mysql Workbench 不读取通过 JDBC 插入的记录

MySQL根据列值计算成功率