SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: How to sum

Hybrid View

  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,087
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post How to sum

    Hi can i ask some help how can i sum the amount and group by manufacturer

    and i want only to sum if the order_id is equal to 0001
    if i have this tables

    product_group
    Code:
        prod_code        productname           manufacturer                   manuf_code
      
        A001                       laptop                 samsung                         001
        B002                      mouse                  Neo                                005
        A002                      monitor(LED)         samsung                          001
        B001                      keyboard              hp                                  003

    orderdetail

    Code:
      order_id        ord_prodcode           ord_desc                         invqty            price           amount
       0001            A001                      laptop                                5                   100             500
       0001            B002                      mouse                               2                      50            100
      0001             A002                      monitor(LED)                      5                   250             1250
      0004            B001                      keyboard                             5                  25                125

    orderheader

    Code:
       orderdate            order_id
       10-12-13            0001
        10-12-13           0001
       10-12-13            0001
       9-12-13             0001
    I dont have idea on how to do this in php,,I hope you can help me thank you in advance.

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Simple. You dont do it in PHP.

    You do it in your query instead.

    GROUP BY orderdetail.order_id,product_group.manufacturer
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Location
    OH, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    like that
    Code:
    select g.manufacturer, SUM(d.amount) as total
    from orderdetail d
    inner join product_group g
    on d.ord_prodcode=g.prod_code
    where d.order_id='0001'
    group by g.manufacturer

  4. #4
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,087
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    @ StarLion,@gk53, Thank you for the reply but i tried to display the data but it did not work when i query this in wampserver it shows the data....i am confuse why it did not display

    Code:
    $sql=mysql_query("select g.manufacturer, SUM(d.amount) as total
    from orderdetail d
    inner join product_group g
    on d.ord_prodcode=g.prod_code
    where d.order_id='0001'
    group by g.manufacturer");
    
    while($row =  mysql_fetch_array($sql)){
       
              echo "<td>row['amount']</td>
              echo <td>row['manufacturer']</td>";
    
    }

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Location
    OH, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the column name not row['amount'] it should be row['total']

  6. #6
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,087
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gk53 View Post
    the column name not row['amount'] it should be row['total']
    HI,it's working ,,,thank you so much for helping me.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •