3 回答
TA贡献1875条经验 获得超3个赞
$select = "agents.person_id, CONCAT(people.first_name, ' ', people.last_name) as last_name, SUM(loans.referral_amount) as referral_amount, COUNT( DISTINCT loans.customer_id ) as no_customers, people.phone_number";
$this->db->select($select, false);
$this->db->from('agents');
$this->db->join('people', 'agents.person_id = people.person_id', 'LEFT');
$this->db->join('loans', 'agents.person_id = loans.referral_agent_id', 'LEFT');
$this->db->where('loans.loan_status', "paid");
$this->db->where('loans.delete_flag', 0);
$this->db->where('agents.deleted', 0);
$result = $this->db->get();
TA贡献1810条经验 获得超4个赞
弄清楚了
$select = "c19_agents.person_id, referral_sum.user_referral_amount, CONCAT(c19_people.first_name, ' ', c19_people.last_name) as agents_name, customer_count.no_customers, referral_sum.user_referral_amount, phone_number";
$this->db->select($select, false);
$this->db->from('agents');
$this->db->join('people', 'agents.person_id=people.person_id', 'LEFT');
$this->db->join('(SELECT c19_loans.referral_agent_id, SUM(c19_loans.referral_amount)
as user_referral_amount
FROM c19_loans
WHERE c19_loans.delete_flag = 0 AND c19_loans.loan_status = "paid"
GROUP BY c19_loans.referral_agent_id) referral_sum', 'c19_agents.person_id = referral_sum.referral_agent_id', 'LEFT');
$this->db->join('(SELECT c19_loans.referral_agent_id, COUNT( DISTINCT c19_loans.customer_id)
as no_customers
FROM c19_loans
WHERE c19_loans.delete_flag = 0 AND customer_id > 0
GROUP by c19_loans.referral_agent_id) customer_count', 'c19_agents.person_id = customer_count.referral_agent_id', 'LEFT');
$this->db->where('agents.deleted', 0);
TA贡献1818条经验 获得超8个赞
首先,据我所知,您将“标准查询”与“查询生成器”混合在一起,这是仅使用一个的好习惯(最好是查询生成器,以防您切换到另一个数据库引擎)。
同样在第二个连接中,您正在进行“AND”比较,尽管这是有效的,但您可以尝试首先使连接起作用。我认为现在可以正常工作,但请确保调试您的查询打印结果并根据文档修复它,
$select = "agents.person_id, CONCAT(people.first_name, ' ', people.last_name) as last_name, SUM(loans.referral_amount) as referral_amount, COUNT( DISTINCT loans.customer_id ) as no_customers, people.phone_number";
$this->db->select($select, false);
$this->db->from('agents');
$this->db->join('people', 'agents.person_id=people.person_id', 'LEFT');
$this->db->join('loans', 'agents.person_id=loans.referral_agent_id', 'LEFT');
$this->db->where('loans.loan_status', 'paid');
$this->db->where('loans.delete_flag', 0);
$this->db->where('agents.deleted', 0);
$this->db->get();
print_r($this->db->last_query());
database.column(最终建议:每当您使用联接时,请始终在查询中使用符号,这样更容易理解并避免两个数据库具有相同名称的列时出现错误)
- 3 回答
- 0 关注
- 108 浏览
添加回答
举报