2013-02-04 67 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

相關問題