Codeigniter查询生成器加入3表

时间:2015-03-18 20:54:05

标签: codeigniter model-view-controller query-builder

我需要你的帮助,我有这样的表table

我想在点击带有id_product的按钮视图时查看详细产品。 这就是我所做的......似乎我需要在id_product之间单引号..还是有一个来自codeigniter的查询构建器来实现这个?

function get_detail_product($id_product){
        $query = "SELECT 
        p.id_product, p.product_name, p.price, p.discount, c.category, b.brand_name, p.description, p.spesification, p.picture, p.begin_date, p.end_date, p.qty
        FROM tbl_product as p, tbl_category as c, tbl_brand as b 
        WHERE p.id_Category = c.id_Category and p.id_brand = b.id_brand
        and id_product = ".$id_product;
        return $this->db->query($query);
    }

2 个答案:

答案 0 :(得分:3)

CodeIgniter确实有一个可以实现这样的查询绑定器。我没有仔细查看过你的SQL,也不知道它是否有效,但是这样的查询看起来像是这样的:

$this->db->select('p.id_product, p.product_name, p.price, p.discount, c.category, b.brand_name, p.description, p.spesification, p.picture, p.begin_date, p.end_date, p.qty')
      ->from('tbl_product as p, tbl_category as c, tbl_brand as b')
      ->where('p.id_Category = c.id_Category')
      ->where('p.id_brand = b.id_brand')
      ->where('id_product', $id_product);
      $query = $this->db->get();
      return $query;

我对你想要达到的目标有点不清楚,我没有完全理解这个问题。您只是试图让查询工作,或者正在寻求建议尝试将所有信息放在一行?如果你想加入表格,你可以试试:

$this->db->select('*');
    $this->db->from('tbl_product');
    $this->db->join('tbl_category', 'tbl_category.id_category = tbl_products.id_category');
    $this->db->join('tbl_brand', 'tbl_brand.id_brand = tbl_products.id_brand', 'inner');
    $this->db->where('id_product', $id_product);
    $query = $this->db->get();
    return $query;

答案 1 :(得分:0)

看起来你没有正确归还它。试试这个。 (未经测试)

$this->db->select('p.id_product, p.product_name, p.price, p.discount, c.category, b.brand_name, p.description, p.spesification, p.picture, p.begin_date, p.end_date, p.qty');
$this->db->join('tbl_category AS c', 'p.id_Category = c.id_Category');
$this->db->join('tbl_brand AS b', 'p.id_brand = b.id_brand');

$query = $this->db->get_where('tbl_product AS p', array('p.id_product' => $prod_id));

return $query->result_array();
相关问题