如何使用codeigniter活动记录编写与此类似的查询?

时间:2011-08-24 13:45:01

标签: php codeigniter

这是我想用codeigniter编写的mysql查询

SELECT r.reciept_no,r.loan_no,r.amount_paid,c.mem_name  
FROM receipts r, customer c 
WHERE r.loan_no = c.loan_no AND reciept_no = '$receipt_id'

任何帮助都会很棒

此致

2 个答案:

答案 0 :(得分:0)

脱离我的头顶

$this->db->select('receipts.reciept_no');
$this->db->select('receipts.loan_no');
$this->db->select('receipts.amount_paid');
$this->db->select('receipts.reciept_no');
$this->db->select('customer.mem_name');
$this->db->where('reciept_no', $receipt_id);
$this->db->join('customer', 'receipts.loan_no = customer.loan_no');
$this->db->get('receipts');

这里的JOIN关键点

答案 1 :(得分:0)

只是一个小小的修改:

$sql = 'r.reciept_no,r.loan_no,r.amount_paid,r.reciept_no,c.mem_name';

$this->db->select($sql)
->where('reciept_no', $receipt_id)
->join('customer as c', 'r.loan_no = c.loan_no')
->get('receipts as r');