选择嵌套查询CodeIgniter

时间:2017-11-24 05:13:54

标签: php mysql codeigniter codeigniter-3

请帮忙。如何在CodeIgniter查询生成器中查询它。我不知道如何在codeigniter中嵌套查询

{{1}}

由于

3 个答案:

答案 0 :(得分:0)

使用$sql = "SELECT * ....."; # your formatted SQL query $this->db->query($sql);

{{1}}

阅读Regular Queries in codeigniter.com

答案 1 :(得分:0)

您可以按以下方式重写查询

$this->db->select("c.*,
    MAX(CASE WHEN s.domainValue = 'CEDC' THEN IFNULL(s.value, 0) ELSE 0 END) as CEDC,
    MAX(CASE WHEN s.domainValue = 'PEDC' THEN IFNULL(s.value, 0) ELSE 0  END) PEDC,
    MAX(CASE WHEN s.domainValue = 'TPC' THEN IFNULL(s.value, 0)  ELSE 0  END) TPC,
    MAX(CASE WHEN s.domainValue = 'SUAL' THEN IFNULL(s.value, 0) ELSE 0  END) SUAL,
    MAX(CASE WHEN s.domainValue = 'PAGBILAO' THEN IFNULL(s.value, 0) END ELSE 0 ) PAGBILAO", FALSE)
            ->from('customer as c')
            ->join("salescontractdetail as s", "c.customerId = s.salesContractId", "left")
            ->group_by("c.customerId")
            ->get()
;

使用活动记录可以写成如下所示

{{1}}

答案 2 :(得分:0)

$this->db->select('"c.*,
    MAX(CASE WHEN s.domainValue = 'CEDC' THEN IFNULL(s.value, 0) ELSE 0 END) as CEDC,
    MAX(CASE WHEN s.domainValue = 'PEDC' THEN IFNULL(s.value, 0) ELSE 0  END) PEDC,
    MAX(CASE WHEN s.domainValue = 'TPC' THEN IFNULL(s.value, 0)  ELSE 0  END) TPC,
    MAX(CASE WHEN s.domainValue = 'SUAL' THEN IFNULL(s.value, 0) ELSE 0  END) SUAL,
    MAX(CASE WHEN s.domainValue = 'PAGBILAO' THEN IFNULL(s.value, 0) END ELSE 0 ) 
    PAGBILAO", FALSE);
        $this->db->FROM('customer as c');
        $this->db->join('salescontractdetail s', 'c.customerId = 
        s.salesContractId','left');
        $this->db->group_by("c.customerId");

        $query = $this->db->get()->result_array();

        return $query;