【问题标题】:how to group a column only if a certain status is true仅当某个状态为真时如何对列进行分组
【发布时间】:2022-01-11 11:10:24
【问题描述】:
$this->db->select('C.product_id,
                   A.customer_name,
                   A.contact_number,
                   A.email_id,
                   A.country_area_id,
                   A.sub_area_id,
                   A.building_no,
                   A.landmark,
                   A.alternative_number,
                   A.timing,
                   A.order_comments,
                   A.latitude,
                   A.longitude,
                   A.shipping_charge,
                   B.stock_item_stock,
                   C.bundle_status,
                   C.bundle_product_id');
        
$this->db->from('tbl_inventory_items as C');
        
$this->db->join('tbl_stock as B','B.stock_item_id=C.product_id and B.stock_status=0','left');
        
$this->db->join('tbl_inventory as A','C.inventory_id=A.inventory_id','left');
        
$this->db->where('C.inventory_id',$inv_ID);

这里如果 C.bundle_status=3 我想对 C.bundle_product_id 分组,如果没有,不需要分组

【问题讨论】:

  • 如果结果也包含 bundle_status= 3 和其他状态怎么办?
  • 还有其他状态。
  • 所以同样的查询我们不能做,那么你需要两个查询。

标签: sql codeigniter query-builder


【解决方案1】:

使用这个

$this->db->group_start()->where('C.bundle_status',3)->group_by('C.bundle_product_id )->group_end()

或创建 else 条件

->or_group_start()->where_not_in('C.bundle_status',3)->group_end()

【讨论】:

    猜你喜欢
    • 2021-06-05
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多