如何对codeigniter和mysql表中的数量求和

khbbv19g  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(283)

我对codeigniter还很陌生,还在探索它。我只想问一下,如何根据产品id添加所有库存量。我有五个相互关联的表,产品、品牌、类别、大小和库存。
类别、品牌和大小是product表上的外键,而product表是stocks表上的外键。
每次有库存时,特定产品的数量和日期都会保存到数据库中。我想知道如何合计特定产品的所有库存并显示所有的总数量。
数据库:产品表

CREATE TABLE `products` (
  `prod_id` int(11) NOT NULL AUTO_INCREMENT,
  `prod_code` varchar(45) NOT NULL,
  `prod_desc` varchar(55) NOT NULL,
  `brand_id` int(11) DEFAULT NULL,
  `category_id` int(11) NOT NULL,
  `size_id` int(11) DEFAULT NULL,
  `original_price` decimal(10,2) NOT NULL,
  `date_registered` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `date_modified` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`prod_id`),
  KEY `category_fk_idx` (`category_id`),
  KEY `size_fk_idx` (`size_id`),
  KEY `brand_id_idx` (`brand_id`),
  CONSTRAINT `brand_id` FOREIGN KEY (`brand_id`) REFERENCES `brand` (`brand_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `category_fk` FOREIGN KEY (`category_id`) REFERENCES `category` (`category_id`),
  CONSTRAINT `size_fk` FOREIGN KEY (`size_id`) REFERENCES `sizes` (`size_id`)
)

库存表

CREATE TABLE `stocks` (
  `stock_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) DEFAULT NULL,
  `quantity` double DEFAULT NULL,
  PRIMARY KEY (`stock_id`),
  KEY `prod_fk_idx` (`product_id`),
  CONSTRAINT `prod_fk` FOREIGN KEY (`product_id`) REFERENCES `products` (`prod_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)

我的视图(product\u view.php)

<div class="col-sm-10" id="main">

    <div class="table-wrapper">
       <div id="content">
           <?php echo $this->session->flashdata('successMessage');
                echo $this->session->flashdata('errorMessage'); ?>
<legend class="text-danger"><h1>Product List</h1></legend>
            <?php   $tableAttr = array(
                    'table_open' => '<table class="table table-responsive table-striped table-hover" id="item_tbl">',
                    );
                                    $item_table = $this->table->set_heading('No.','PRODUCT CODE','PRODUCT DESCRIPTION','BRAND', 'CATEGORY', 'SIZE','QUANTITY','ACTION');
                $item_table = $this->table->set_template($tableAttr);
                $num = 0;
                foreach ($items as $item) {
                    $itemName = urlencode($item->prod_desc);
                    $num++;
                    $item_table = $this->table->add_row($num, $item->prod_code, $item->prod_desc, $item->brand_name,$item->category_name,$item->size,$item->quantity,"

                        <a href='".base_url("item/update/$itemName")."'><button class='btn btn-info btn-sm'>EDIT</button></a> 
                                                    <a href='".base_url("item/stock_in/$itemName")."'><button class='btn btn-primary btn-sm'>STOCK IN</button></a> 
                        ");
                }
                echo $this->table->generate($item_table);  ?>
                </div>

</div>                            
</div>

控制器

public function home () {
    $this->load->model('item_model');

    $data['items'] = $this->item_model->itemList();
    $data['page'] = 'home';

    $this->load->view('header',$data);
    $this->load->view('side_menu');
    $this->load->view('product_view',$data);
    $this->load->view('footer');
}

模型(item\u model.php)

public function itemList () {
    $this->load->database();

$this->db->select('p.*,c.category_name,s.size,b.brand_name,t.quantity')

            ->from('stocks t');
              $this->db->join('products p','p.prod_id = t.product_id','full')
                    ->join('category c','p.category_id = c.category_id','full');
               $this->db->join('sizes s','p.size_id = s.size_id','full');
                $this->db->join('brand b','p.brand_id = b.brand_id','full');

            $result=$this->db->get()->result();

    return $result;
}

我想/我的意思是在我的网站上显示

+-----+--------------+--------------+-------------------+
| ID  | Product Code | Description  | Stocks Quantity   |
+-----+--------------+--------------+-------------------+
| 001 |   12345      | sample       |  12               |
| 002 |   23456      |  try         |  15               |
+-----+--------------+--------------+-------------------+

在我的数据库表中

+-----+--------------+--------------+
| ID  | Product_Id   | Quantity     |
+-----+--------------+--------------+
| 1   |   1          |     10       | 
| 2   |   3          |     7        |
+-----+--------------+--------------+
| 3   |   1          |     2        | 
| 4   |   3          |     8        |
+-----+--------------+--------------+

我希望你能帮助大家。

dwthyt8l

dwthyt8l1#

首先,在控制器中,您需要像以前一样加载模型

$this->load->model('database');

因此,请将模型名称更改为其他名称以避免歧义第二:如果要加载数据库库,我不知道为什么要在模型类中加载相同的模型。像这样装

$this->load->library('database');
p4tfgftt

p4tfgftt2#

使用sum()和group by:

$this->db->select('p.*,c.category_name,s.size,b.brand_name,SUM(t.quantity) as total_quantity',false)
               ->from('stocks t')
               ->join('products p','p.prod_id = t.product_id','full')
               ->join('category c','p.category_id = c.category_id','full')
               ->join('sizes s','p.size_id = s.size_id','full')
               ->join('brand b','p.brand_id = b.brand_id','full')
               ->group_by('t.product_id');
          $result=$this->db->get()->result();

相关问题