Reputation: 1399
i want to check inside where condition that are results of another query.
in English+sql this where
condition is like this
where(table.field1=$data[field1] , this $data[field1] should be only from some another query results from another results.)
. i have tried this
if( isset($data['field1']) && $data['field1'] != '' ) {
$this->db->where('table.field1',$data['field1'] IN (SELECT id FROM table2' WHERE id=2') );
}
but this causes error
Upvotes: 1
Views: 1335
Reputation: 5506
public function get_category_with_all_sub() {
$cquery = $this->db->get ( 'category' );
$cresults = $cquery->result ();
$return = array ();
foreach ( $cresults as $c ) {
$category_name = $c->name;
$this->db->select ( '*' );
$this->db->from ( 'sub_category' );
$this->db->where ( 'category_id', $c->id );
$squery = $this->db->get ();
$sresults = $squery->result ();
foreach ( $sresults as $s ) {
$return [$category_name] [] = array (
'id' => $s->id,
'name' => $s->name,
'desc' => $s->description
);
}
}
return $return;
}
Please build your model function like this, $c->id
is coming from another results.
Upvotes: 1
Reputation: 564
try to do it by that way
if( isset($data['field1']) && $data['field1'] != '' ) {
$this->db->where('table.field1',$data['field1']);
$this->db->where('table.field1 in ','(SELECT id FROM table2 WHERE id=2)',false);
}
Upvotes: 1