Oh my head's melted. Help would be appreciated.
Right, the following code:
This works wonderfully where there is only 1 product from the group in_stock as there's only 1 to update as for_sale.Code:$query = 'SELECT autoInc, productId, COUNT(*) FROM stock WHERE in_stock = "yes" GROUP BY productId HAVING COUNT(*) = "1"'; $results = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array( $results )) { mysql_query('UPDATE stock SET for_sale = "yes" WHERE autoInc = "'.$row["autoInc"].'"') or die(mysql_error()); }
However, when we have 2 or more with the same price - we only need one updated as for_sale and either will do, but not both.
The update statement is the problem!Code:$query = 'SELECT autoInc, MIN(price), search_price, brand, product, productId, COUNT(*) FROM stock WHERE in_stock = "yes" GROUP BY productId HAVING COUNT(*) > "1"'; $results = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array( $results )) { ...
This updates both rows and I can't stick a LIMIT on an UPDATE so they both go to for_sale. How do I limit it to just 1?Code:mysql_query('UPDATE stock SET for_sale = "yes" WHERE autoInc = "'.$row["autoInc"].'" AND price = "'.$row["MIN(price)"].'"') or die(mysql_error()); }



Reply With Quote

Bookmarks