首页 > 解决方案 > 在选定的数据库表列中查找选定标识符的最后一个条目的输入

问题描述

我的桌子上有一个列,旨在返回我为特定公司工作的最后日期。对于我的查询,我不认为我可以依赖时间戳,因为一些条目是提前输入到系统中的,例如我今天可能为一家公司工作,下周重新签约,我会更新这两个工作(今天和下周) 今天工作完成后。

表 1 的形式(按日期排列,不一定是填充日期(时间戳))

 companyname  |  job  |  date    |  payment  | cost   
==================================================== 
 Company A    | JobA  | 01/09/19 |   £100    |  £50 
 Company B    | JobB  | 02/09/19 |   £100    |  £50 
 Company A    | JobC  | 03/09/19 |   £100    |  £50 
 Company B    | JobD  | 04/09/19 |   £100    |  £50 
 Company A    | JobE  | 05/09/19 |   £100    |  £50 
 Company B    | JobF  | 06/09/19 |   £100    |  £50 
 Company A    | JobG  | 07/09/19 |   £100    |  £50   

表 2

 companyname  |  income  |  profit  | avgmarkup | lastjob   
==========================================================
 Company A    |          |          |            |  
 Company B    |          |          |            |   

目前,我能够填充收入和利润字段,但对Avg MarkupLast job字段感到困惑

填充表 2 的当前代码

<table id="client-table" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th class="client-id">ID</th>
            <th>companyame</th>
            <th>income</th>
            <th>profit</th>
            <th>avgmarkup</th>
            <th>lastjob</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $sql = "SELECT `Table2`.*, SUM(`Table1`.`payment`) AS payment,
                SUM(`Table1`.`payment` - `Table1`.`cost`) AS profit,
                SUM(((`Table1`.`payment` - `Table1`.`cost`) / `Table1`.`cost`)*100) AS markup
                FROM `Table2` 
                LEFT JOIN `Table1` ON `Table1`.`customer` = `Table2`.`Table2` 
                GROUP BY `Table2`.`id`";
        $query = mysqli_query($conn, $sql);

        if (mysqli_num_rows($query) > 0) {
            // output data of each row
            while($result = mysqli_fetch_assoc($query)) {
                echo "<tr>
                <td class='client-id'>".$result['id']."</td>
                <td>".$result['company']."</td>   <----- Pulled from database table
                <td>".$result['payment']."</td>
                <td>".$result['profit']."</td>
                <td>".$result['markup']."</td> <-----Markup not giving correct answer
                <td></td> <------Last job date should go here
                </tr>";
            }
        } else { 
            echo "0 results";
        }
        mysqli_close($conn);
        ?>
    </tbody>
</table>

标记公式为:

((money_in - money_out) / money_out)*100

所以拿这个我用过-a above

SUM(((`Table1`.`payment` - `Table1`.`cost`) / `Table1`.`cost`)*100) AS markup

哪个没有给出真正的答案......

但是,我的主要查询是基于上一份工作。

我已经date在数据库中设置了日期类型,它应该被识别为......

我有没有办法将每个公司链接到表 1 中为他们显示的最后日期并在表 2 中显示该日期?

https://www.db-fiddle.com/f/oxX5DdKdiMF9fqQy1yNqF7/16

对不起,如果啰嗦,我希望有人可以帮助:-)

标签: phpmysql

解决方案


按照我们的谈话。这是您的数据的查询。

SELECT `Table2`.*, SUM(`Table1`.`payment`) AS money_in,
SUM(`Table1`.`payment` - `Table1`.`cost`) AS profit,
SUM(`Table1`.`payment` - `Table1`.`cost`) / SUM(`Table1`.`cost`)*100 AS markup,
MAX(`Table1`.`date`) AS `lastjob`
FROM `Table2` 
LEFT JOIN `Table1` ON `Table1`.`company` = `Table2`.`companyname` 
GROUP BY `Companyname`,`id`;

这是相同的小提琴

请更新您的 php 代码以在前端显示它。

while($result = mysqli_fetch_assoc($query)) {
                echo "<tr>
                <td class='client-id'>".$result['id']."</td>
                <td>".$result['company']."</td>   <----- Pulled from database table
                <td>".$result['payment']."</td>
                <td>".$result['profit']."</td>
                <td>".$result['markup']."</td> <-----Markup not giving correct answer
                <td>".$result['lastjob']."</td> <------Last job date should go here
                </tr>";
            }

推荐阅读