Welcome Guest, Not a member yet? Register   Sign In
A confirm about a SQL query
#2

The Query Builder options in CI offer a good method to create sub-queries:
PHP Code:
if ($for_category) {
 
 $this->db
  
->select('category')
 
 ->from('categories')
 
 ->like('category',$for_category);
 
 $subquery $this->db->get_compiled_select();
}

$this->db->select('p.product,b.brand');
$this->db->from('products p');
$this->db->join('brands b','b.id = p.brand_id');
if (
$for_category) {
 
  $this->db->join('categories_to_product ctp','ctp.category_id = p.id');
 
  $this->db->join('categories c','ctp.category_id = c.id');
 
  $this->db->where("ctp.category IN ($subquery)",NULL,FALSE);
 
  //not tested: $this->db->where_in('ctp.category',$subquery);
}
$this->db->like('p.product',$for_product);
$this->db->like('b.brand',$for_brand);
$query $this->db->get();
if (
$query->num_rows() > 0) {
 
 $records $query->result_array();

Reply


Messages In This Thread
A confirm about a SQL query - by gbwebapps - 02-28-2017, 03:38 PM
RE: A confirm about a SQL query - by Wouter60 - 03-01-2017, 06:40 AM
RE: A confirm about a SQL query - by gbwebapps - 03-01-2017, 08:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB