在带有多个表的Codeigniter中进行内部联接

时间:2019-03-28 04:20:21

标签: codeigniter

我想在codeigniter的Crudodel中创建一个显示功能,以显示第13号发票上的记录。我将在SQL上运行以下查询。它按照我的要求工作。现在,我必须使用内部联接在codeigniter中转换此查询。

SELECT b1.Invoice_No,
       cust_name,
       cust_address,
       cust_contact,
       Item_name,
       Item_qty,
       Item_amount
FROM tbl_bill_invoice AS b1
INNER JOIN tbl_billmenu AS b2 ON(b2.Invoice_No = b1.Invoice_No)
INNER JOIN tbl_billcustomer AS b3 ON(b3.cust_id = b1.cust_id)
WHERE b1.Invoice_no = 13

3 个答案:

答案 0 :(得分:0)

尝试一下

$this->db->select('b1.Invoice_No, cust_name, cust_address, cust_contact, Item_name, Item_qty, Item_amount');
$this->db->from(' tbl_bill_invoice as b1');
$this->db->join('tbl_billmenu as b2', 'b2.Invoice_No = b1.Invoice_No', 'inner');
$this->db->join('tbl_billcustomer as b3', 'b3.cust_id = b1.cust_id', 'inner');
$this->db->where('b1.Invoice_no', 13);
return $this->db->get()->row();

答案 1 :(得分:0)

看看,

$invoice_no = 13;
$this->db->select('b1.Invoice_No, cust_name, cust_address, cust_contact, Item_name, Item_qty, Item_amount');
$this->db->from('tbl_bill_invoice b1');
$this->db->join('tbl_billmenu b2', 'b2.Invoice_No = b1.Invoice_No', 'inner');
$this->db->join('tbl_billcustomer b3', 'b3.cust_id = b1.cust_id', 'inner');
$this->db->where('b1.Invoice_no', $invoice_no);
$query = $this->db->get();
if ($query->num_rows() > 0) {
    return $query->row();
} else {
    return array();
}

答案 2 :(得分:0)

只需编写

$sql = "SELECT b1.Invoice_No,
   cust_name,
   cust_address,
   cust_contact,
   Item_name,
   Item_qty,
   Item_amount
   FROM tbl_bill_invoice AS b1
   INNER JOIN tbl_billmenu AS b2 ON(b2.Invoice_No = b1.Invoice_No)
   INNER JOIN tbl_billcustomer AS b3 ON(b3.cust_id = b1.cust_id)
   WHERE b1.Invoice_no = 13";
$result = $this->db->query($sql)->result();