Counting Value From The Mysql Table

Discussion in 'PHP' started by ariffin246, Jul 27, 2010.

  1. #1
    I have two different tables "order" and "item" which i joined using left join.

    In order table there is a column name called quantity and in item table, there is a column called price.

    What i need to do is multiply the quantity from order with the price from item then get a total value.

    There will be several order records added together for the final sum...


    Can anyone point me, how to code the counting part in php.
     
    ariffin246, Jul 27, 2010 IP
  2. learnerabn

    learnerabn Peon

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You are multiplying quantity and price.
    which one do u want to count?
     
    learnerabn, Jul 27, 2010 IP
  3. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #3
    You make no reference to your structure and why you are using left join as opposed to an
    inner join; if you should actually be using inner then you do not need the coalesce in the
    statement below;

    it will fetch the price on the orderid you specify in the where clause;


    SELECT SUM(COALESCE(o.quantity, 1) * COALESCE(i.price,0)) as total 
    FROM orders as o
    LEFT OUTER JOIN items as i ON (o.itemid=i.itemid) 
    WHERE o.orderid = 1
    GROUP BY o.orderid;
    Code (markup):
     
    lukeg32, Jul 27, 2010 IP