2017-04-11 229 views
1

我正致力於從不同的表中獲取客戶詳細信息。我已經創建並測試了這個查詢,它工作正常。問題是如何將用戶(轉換)爲Active Query查詢。謝謝。如何使用相關子查詢進入Codeigniter中的活動記錄查詢

  SELECT c.cust_id, `c`.`cust_contact_name`, `c`.`cust_active_flag`, 
      (select COUNT(`pm`.`cust_id`) from property_master as pm 
      Where pm.cust_id = c.cust_id) as prop_count, 
      (select a.addr_phoneno1 from address as a 
      WHERE a.cust_id = c.cust_id AND a.addr_type_flag IN ('C', 'CP')) as cust_phone, 
      (select count(ci.cust_id) from customer_inquiry as ci 
      WHERE ci.cust_id = c.cust_id) as inquiry_count, 
      (select max(inq_date) from customer_inquiry as ci 
      WHERE ci.cust_id = c.cust_id) as last_inq_date 
      FROM customer as c 
      GROUP BY c.cust_ID 
+0

@hekmat你可以在這個先生感謝 –

回答

1

CodeIgniter活動記錄類不直接支持子查詢。您可以使用任何第三方庫,如NTICompass CodeIgniter-Subqueries$this->db->query();來執行您的查詢。

不過,您可以使用以下方法。但我會建議使用$this->db->query();

$this->db->select('c.cust_id, `c`.`cust_contact_name`, `c`.`cust_active_flag`, 
      (select COUNT(`pm`.`cust_id`) from property_master as pm ' Where pm.cust_id = c.cust_id) as prop_count, (select a.addr_phoneno1 from address as a 
      WHERE a.cust_id = c.cust_id AND a.addr_type_flag IN ('C', 'CP')) as cust_phone, 
      (select count(ci.cust_id) from customer_inquiry as ci 
      WHERE ci.cust_id = c.cust_id) as inquiry_count, 
      (select max(inq_date) from customer_inquiry as ci 
      WHERE ci.cust_id = c.cust_id) as last_inq_date) 
     ->from('customer c'); 
$this->db->group_by('c.cust_ID'); 
$result = $this->db->get(); 
+0

幫助@Bikram但我尋找比直接使用查詢其他選項。 –

+0

您可以使用任何第三方CI庫。 –

+0

你可以建議任何人或直接鏈接看看。 –