Forum Moderators: coopster

Message Too Old, No Replies

PHP Duplicate Help

php, mysql, duplicates

         

phpmysql

8:44 pm on Jan 10, 2008 (gmt 0)

10+ Year Member



I'll try to explain the scenerio. I have a mysql table with rows that have columns of ordNum and shippingCost. When I pull the results from the table, sometimes I get two rows with duplicate ordNum with two different shippingCost.(this happens when the order takes two boxes). I would like to have the two ordNum that are duplicates combine and add the two shippingCost together. If anyone could help please reply.

Sample Code:

<?php

if (isset($_POST['submit'])) {

$number1 = $_POST[number1];
$number2 = $_POST[number2];

$select = mysql_query("ORDHEADER.ordNum, packages.shippingCost
FROM ORDHEADER LEFT JOIN packages ON ORDHEADER.ordNum = packages.ordNum WHERE ORDHEADER.ordNum BETWEEN $number1 AND $number2");

$color ="1";

echo "<b>Range: $number1 - $number2 </b><br />";

echo "<table border='1' cellpadding='2'>
<tr>";
echo "<th>Order Number</th>";
echo "<th>Shipping Cost</th>";
echo "</tr>";

while($row = mysql_fetch_array($select))
{
if($color==1){
echo "<tr bgcolor='#CCFF99'>";
echo "<td>" .$row['ordNum']."</td>";
echo "<td>" .$row['shippingCost']."</td>";
echo "</tr>";

$color="2";
}

else{
echo "<tr bgcolor='#FFFFFF'>";
echo "<td>" .$row['ordNum']."</td>";
echo "<td>".$row['shippingCost']."</td>";
echo "</tr>";

$color="1";
}
}

echo "</table>";

mysql_close();

}

?>

venelin13

7:50 am on Jan 11, 2008 (gmt 0)

10+ Year Member



Hello,
here is the query you need:


SELECT
ordheader.ordnum,
sum( packages.shippingcost ) AS shipping
FROM
ordheader, packages
WHERE
(ordheader.ordnum = packages.ordnum)AND
(ordheader.ordnum BETWEEN '$number1' AND '$number2')
GROUP BY ordheader.ordnum

phpmysql

3:57 pm on Jan 11, 2008 (gmt 0)

10+ Year Member



Thanks for your help venelin13....I was trying to do this with php code, but your mysql query made things easier.

Thanks Again