1

I've asked a question regarding my SQL Query a while back and it worked fine until I noticed I had forgotten a very important piece to the code and now I've spent about an hour and a half trying to modify my code. The original question asked a good way to inner join two tables (orders and order_items). I then did a mysql_num_row() over the SQL Query and called it a day. I forgot that there's a cell in my order_items table named quantity. I need to integrate this into my count. I'm including my code below, and any ideas on how to easily implement this would be appreciated.

$SQL2ORDERSEARCH = "
SELECT * FROM order_items
INNER JOIN orders
ON orders.id = order_items.ord
WHERE orders.session = '$sessionID'
AND order_items.dish = '$SEARCHITEMS_OBJECT->dish'
AND order_items.size = 'full'";

$ORDERSEARCH = mysql_query($SQL2ORDERSEARCH) or die(mysql_error());
$ORDERSEARCH_NUM_ROWS = mysql_num_rows($ORDERSEARCH);
$FULLTOTAL = $FULLTOTAL + $ORDERSEARCH_NUM_ROWS;

I tried attempting a different route by doing a count. So I modified my Query as such:

$SQL2ORDERSEARCH = "
SELECT COUNT(quantity) FROM order_items
INNER JOIN orders
ON orders.id = order_items.ord
WHERE orders.session = '$sessionID'
AND order_items.dish = '$SEARCHITEMS_OBJECT->dish'
AND order_items.size = 'full'";

I don't technically need anything else since all I am trying to do is count how many dishes are involved here. I then created an if statement to figure if there was any rows coming from this query, and then calculate the quantity of them.

if($ORDERSEARCH_NUM_ROWS > 0 ) {
   while($ORDERSEARCH_OBJECT = mysql_fetch_object($ORDERSEARCH)) {
      $FULLQTY = COUNT($ORDERSEARCH_OBJECT->quantity);
   }
}

I've gotten mixed results. Sometimes I get just straight 1's down the data table. Other times (depending on any small changes such as $FULLQTY += COUNT($ORDERSEARCH_OBJECT->quantity); and trying that) I get results where it seems almost pattern-like with the numbers increasing by 5+ and somehow adding up to near 20+ the further down the list you go.

I'm just looking for an easy way to get the count of the quantity cell in order_items, displaying them down a table, and then calculating a total. I have everything fine and dandy minus getting the count of the quantity cell in order_items. Any ideas, I'd greatly appreciate it!

4

1 に答える 1

2

単純なselect sum(quantity)はここで機能しませんか?

于 2012-05-22T15:59:23.617 に答える