我在尝试添加第三张桌子时遇到了一些小麻烦。
这是表结构:
products =
id name code
5 product1 002
522 product1 002
warehouses =
id code name address city
1 store1 store1 store1 store1
warehouses_products =
id product_id warehouse_id quantity
2 5 1 -3
3 522 1 -2
4 446 1 0
这是获取数据的函数:
$this->load->library('tables');
$this->tables
->select("products.id as productid, name, code (CASE WHEN sum(warehouses_products.quantity) Is Null THEN 0 ELSE sum(warehouses_products.quantity) END) as totalQuantity, warehouses.name");
$this->tables->from('products');
$this->tables->join('warehouses_products', 'products.id=warehouses_products.product_id', 'left');
$this->tables->join('warehouses', 'warehouses_products.id=warehouses.warehouse_id', 'left');
$this->tables->group_by("products.id");
$this->tables->unset_column('productid');
echo $this->tables->generate();
任何帮助表示赞赏