总和表sql codeigniter上的问题

时间:2013-02-08 17:20:51

标签: sql codeigniter

当我想用id来计算列的总和时,我有一点问题。

表产品

id   code    name        price  quantity
522  123     Product 1     12      4

表purchase_items

id  product_id  quantity
1      522            5
2      522            1

表sale_items

id  product_id  quantity
1      522            1
2      522            1

功能codeigniter:

$products = $this->alerts->select('products.id as productid, products.code as code, products.name, products.price, sum(sale_items.quantity)')
      ->from('products');
      $this->alerts->join('sale_items', 'products.id = sale_items.product_id');           
      $this->alerts->group_by("products.id");

      $products = $this->alerts->select('products.id as productid, products.code as code, products.name, products.price, sum(purchase_items.quantity)')
      ->from('products');
      $this->alerts->join('purchase_items', 'products.id = purchase_items.product_id');           
      $this->alerts->group_by("products.id");


        $this->alerts->unset_column('productid');

        foreach ($this->result_array() as $product)
        {
            $products[] = $product['record'];
        }

        return $products;
    }

问题在于

的结果
purchase_items the result is 12
sale_items      the result is 4

为什么取结果x 2?

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

因为您通过商品主数据(产品表)将购买活动加入销售活动。因此,总和中包含的购买行数取决于您拥有的销售行数;销售额反之亦然。

您需要将这些查询分隔为在视图中组合的单独SQL语句,或者您可以使用SQL UNION ALL组合两个语句:一个用于购买,一个用于销售。

两个陈述:

$this->alerts
            ->select('products.id as productid, products.code as code, products.name, products.price, sum(purchase_items.quantity)')
            ->from('products');
            $this->alerts->join('purchase_items', 'products.id = purchase_items.product_id');
                $this->alerts->group_by("products.id");
            echo "Total Purchases";
            echo $this->alerts->generate();


 $this->alerts
            ->select('products.id as productid, products.code as code, products.name, products.price, sum(sale_items.quantity)')
            ->from('products');
            $this->alerts->join('sale_items', 'products.id = sale_items.product_id');           
                $this->alerts->group_by("products.id");
            echo "Total Sales";
            echo $this->alerts->generate();

或者,在SQL中:

$results = $this->db->query('
    SELECT products.id as productid, products.code as code, products.name, products.price, ''Purchase'' as transaction, sum(purchase_items.quantity)
    FROM products
    JOIN purchase_items ON products.id = purchase_items.product_id
    GROUP BY products.id
    UNION ALL
    SELECT products.id as productid, products.code as code, products.name, products.price, ''Sale'' as transaction sum(sales_items.quantity)
    FROM products
    JOIN sales_items ON products.id = sales_items.product_id
    GROUP BY products.id')->result_array();

// Echo $results....

修改 我不完全确定你的Alerts模型中有什么,但是在修订后的查询中,你要为变量$products分配两次SQL语句但不执行语句?

另外,请确保您的警报模型扩展为CI_Model

在更标准的Codeigniter语法中,我会这样做:

    // In Controller, assuming the Alerts model is loaded, and using method chaining.

    // Get all sales. By the way, you normally have to group on all columns *not* being aggregated. You might
    // get unexpected results by grouping on price, unless the price never changes.
    $this->alerts->select('products.id as productid, products.code, products.name, products.price, sum(sale_items.quantity) as quantity')
          ->from('products')
          ->join('sale_items', 'products.id = sale_items.product_id')
          ->group_by('products.id, products.code, products.name, products.price');
    $sales = $this->alerts->get()->result_array();

    // Get all purchases
    $this->alerts->select('products.id as productid, products.code, products.name, products.price, sum(purchase_items.quantity) as quantity')
          ->from('products')
          ->join('purchase_items', 'products.id = purchase_items.product_id')
          ->group_by(products.id, products.code, products.name, products.price);
    $purchases = $this->alerts->get()->result_array();

    // Put it all together
    $products = array_merge($sales, $purchases);

    // And now do something with your combined results $products. Send to view, echo etc.