找到“number”列值相同的每一行的两个值之间的差异

时间:2013-02-04 20:01:58

标签: php mysql

month   year    customer    distributor    number   name    price   
10      2012    20          8              2406163 CHEESE   50.4
10      2012    20          8              2325141 APPLE    25.2
11      2012    20          8              2406163 CHEESE   48.1
11      2012    20          8              2325141 APPLE    20.2
12      2012    20          8              2325141 APPLE    23.2
12      2012    20          8              2406163 CHEESE   46.4

我试图比较“数字”是相同值的两个月,我想找出价格之间的差异(如果有的话)。

使用上面的数据,我想比较11个月和11个月。 12(或10和11,取决于这些由用户选择)。因此,对于11和12个月,奶酪的价格差异为 - 1.7美元,苹果为3美元。这就是我想要做的,除了它应该对表中找到的“number”相同的两个月之间的所有行执行此操作。

这是我当前的代码......它目前按项目名称而不是项目编号进行比较。然而,无论出于何种原因,它只能找到200多个项目中3个项目的差异,奇怪的是。

感谢您的帮助。

<?PHP
$from_date = $from_month;
$to_date = $to_month;
$query = " 
   select * from ogi
   where month BETWEEN '" . $from_date . "' AND  '" . $to_date . "' GROUP BY number HAVING count(*) > 1 ;
"; 
try 
{ 
    // These two statements run the query against your database table. 
    $stmt = $db->prepare($query); 
    $stmt->execute(); 
} 
catch(PDOException $ex) 
{ 
    // Note: On a production website, you should not output $ex->getMessage(). 
    // It may provide an attacker with helpful information about your code.  
    die("Failed to run query: " . $ex->getMessage()); 
} 

// Finally, we can retrieve all of the found rows into an array using fetchAll 
$rows = $stmt->fetchAll(); 

//Create a variable to hold the "previous month" values
$previousname = '';
$previousprice = '';

//Check each record from your query
foreach($rows as $row) {

//If not first time around && same item name as previous
if($previousname != '' && $previousname == $row['name']) {

  //subtraction calculation here

  if ($row['price'] <= $previousprice) {
$difference = "(Price Lowered) Price difference of $";
$result = $previousprice - $row['price'];
$percent = round(100.0*($previousprice/$row['price']-1));

   ?>
            <tr>
                <td><?php echo "" . $row['name'] . ""?></td>
                <td><?php echo $difference; ?><?php echo $result ?>        </td>
                <td><?php echo $percent; ?> %</td>

            </tr>
            <?
} elseif ($row['price'] > $previousprice) {
$result = $row['price'] - $previousprice;
$percent = round(100.0*($previousprice/$row['price']-1));
$addition = "(Price Higher) Price difference of $";

 ?>
            <tr>
                <td><?php echo "" . $row['name'] . ""?></td>
                <td><?php echo $addition; ?><?php echo $result ?>    </td>
                <td><?php echo $percent; ?>%</td>

            </tr>
            <?
} 
   }
   else {
   ?>
   <!-- <tr>
                <td><?php echo "" . $row['name'] . ""?></td>
                <td></td>
                <td></td>

            </tr> -->
            <?

}

//Assign the "previous month" value
$previousname = $row['name'];
$previousprice = $row['price'];

}


?>
<?
}
else {
?>
<form action="" method="post">
Choose Months to Compare:<br>
Customer: <input type="text" name="customer"><br>
Month 1: <input type="text" name="from_month"><br>
Month 2: <input type="text" name="to_month"><br>
<input type="submit" value="Compare">
</form>
<?
}
?>

1 个答案:

答案 0 :(得分:1)

如下:

SELECT a.number, a.name, (a.price - b.price) as pdiff
FROM table a, table b
WHERE a.number = b.number 
    AND a.month = montha 
    AND b.month = monthb;

如果我没有犯错,这个查询应该会给你所需的信息:

  

(例如montha = 10和monthb = 11)

希望这有帮助。