1

我有一个包含 3 个表(客户、蜂箱、检查)的数据库,每个检查只能有一个蜂箱和一个客户。一个客户可以有很多蜂箱,一个蜂箱可以有很多检查)

理想情况下,我想做的是查看每个客户的每个蜂箱的最新检查,并计算有多少蜂箱死了,有多少蜂箱还活着(基于检查表中“蜂箱强度”字段的值。

这让我困惑了一段时间,我认为这是因为我的代码很糟糕,而且很可能不是最好的方法。

我想知道是否有人可以帮助我实现下表:

客户名称 | 最近的检查 | 总活荨麻疹 | 总死荨麻疹

这是我的半工作代码(请注意 - 它很丑陋):

    $ContactsQ = mysql_query("
SELECT 
*
FROM 
Contacts
WHERE 
`Contacts`.`Status` = 'Active'
AND 
`Contacts`.`Type` = 'Rental'
GROUP BY
ContactID
");
if (!$ContactsQ) {
    die('Invalid query: ' . mysql_error());
} 
echo "<table class=\"tablesorter\" >";
echo "<thead><tr>";
echo "<th>ID &nbsp&nbsp&nbsp&nbsp</th>";
echo "<th> Name</font></th>";
echo "<th>Live Hives</font></th>";
echo "<th>Dead Hives</font></th>";
echo "<th>Last Inspected</font></th>";
echo "<th>AG</font></th>";
echo "</tr></thead>";   







// For Each Customer//
while(($CQ =  mysql_fetch_assoc($ContactsQ))) {
$live = 0;
$dead = 0;                  
$CID = $CQ['ContactID'];
                    $Query = mysql_query("
                    SELECT 
                    MAX(`InspectionsNZ`.`InsID`) Ins,
                    Hives.* ,
                    Contacts.*
                    FROM
                    `InspectionsNZ`
                    LEFT JOIN `Contacts` ON `Contacts`.`ContactID` = `InspectionsNZ`.`CustomerID`
                    LEFT JOIN `Hives` ON `Hives`.`ID` = `InspectionsNZ`.`HiveID`
                    WHERE
                    `Hives`.`Status` = 'Active'
                    AND Contacts.ContactID = $CID
                    GROUP BY 
                    `ID`
                    ");
                    if (!$Query) {
                                die('Invalid query: ' . mysql_error());
                            } 





                    // For Each Hive the customer has that is active
                    while(($row1 =  mysql_fetch_assoc($Query))) {
                                                                $HID = $row1['ID'];
                                                                $lastinsID = $row1['Ins'];
                                                                $Query1 = mysql_query("
                                                                SELECT 
                                                                HiveStrength, Date, InsID, CustomerID
                                                                FROM 
                                                                InspectionsNZ
                                                                WHERE 
                                                                `InsID` = $lastinsID
                                                                ORDER BY 
                                                                `InsID`
                                                                ");
                                                                if (!$Query1) {
                                                                    die('Invalid query: ' . mysql_error());
                                                                } 






                                                                // Add to total if Alive or Dead
                                                                while(($row =  mysql_fetch_assoc($Query1))) {


                                                                                            //Get current CustomerID//
                                                                                            $nquery = mysql_query("
                                                                                            SELECT 
                                                                                            MAX(`InspectionsNZ`.`InsID`) , 
                                                                                            `CustomerID` 
                                                                                            from 
                                                                                            `InspectionsNZ` 
                                                                                            WHERE 
                                                                                            `HiveID` = $HID
                                                                                            ");
                                                                                            if (!$nquery) {
                                                                                            die('Invalid query: ' . mysql_error());
                                                                                            }
                                                                                            while(($nrow =  mysql_fetch_assoc($nquery))) {
                                                                                                $NCID = $nrow['CustomerID'];
                                                                                            }
                                                                                            //END Get current CustomerID//


                                                                    if ($CID == $NCID){
                                                                    if($row['HiveStrength'] > 0) {
                                                                        $live++;
                                                                    }
                                                                    elseif($row['HiveStrength'] == 0) {
                                                                        $dead++;

                                                                    }
                                                                    }
                                                                    echo $HID." - ".$NCID." - ".$row['CustomerID']." - ".$CID." - ".$lastinsID." - ".$row['Date']."<br/>";

                                                                }


                    }
                    echo "<tr>";
                    echo "<td><h3>" .$CQ['ContactID']. "</h3></td>";
                    echo "<td><a href='CustomerTop.php?cid=" .$CQ['ContactID']. "'>" .$CQ['Name']. "</a></td>";
                    echo "<td><h3>" .$live. "</h3></td>";
                    echo "<td><h3>" .$dead. "</h3></td>";
                    echo "<td><h3>" .$CQ['Date']. "</h3></td>";
                    echo "<td><h3>" .$CQ['RunGroup']. "</h3></td>";

}
echo "</tr>";
echo "</tbody>";
echo "</table><br />";

我得到的是一份客户名单,但对活蜂箱和死蜂箱的计算不正确......

请帮忙 :)

朱利安

4

0 回答 0