2013-02-04 111 views
0
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 

我试图比较两个月的“数量”是相同的价值,我想找到价格如果有的话)。找到“number”列值相同的每行的两个值之间的差异

使用上面的数据,我想比较几个月11(或10和11,这取决于这些是由用户选择)。所以,在几个月里,奶酪的价格差异是 - 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

什么是这样的:

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例如)

希望这有助于。

+0

太棒了!太棒了。谢谢。 – Alex

+0

很高兴它帮助:) – Odinn

+0

你知道我会怎么去尝试在页面上回显“a.price”或“b.price”吗?我试过'<?php echo“”。 $ row ['a.price']。 “”?>'(和b.price),但都不起作用。我不太熟悉mysql,所以我确信我做错了什么。 – Alex

相关问题