Using Mysql WHERE IN clause in codeigniter
Asked Answered
I

5

18

I have the following mysql query. Could you please tell me how to write the same query in Codeigniter's way ?

SELECT * FROM myTable 
         WHERE trans_id IN ( SELECT trans_id FROM myTable WHERE code='B') 
         AND code!='B'
Industrialist answered 13/6, 2012 at 21:7 Comment(3)
did you use subquery in codeigniter as i postedAnatolic
Yes, I have managed to use it successfully few minutes ago.. :) Thanks :)Industrialist
@raheelshan. Would you please check this post. https://mcmap.net/q/358427/-mysql-subtracting-values-based-on-two-queries/1209690 Thanks :)Industrialist
A
25

You can use sub query way of codeigniter to do this for this purpose you will have to hack codeigniter. like this
Go to system/database/DB_active_rec.php Remove public or protected keyword from these functions

public function _compile_select($select_override = FALSE)
public function _reset_select()

Now subquery writing in available And now here is your query with active record

$this->db->select('trans_id');
$this->db->from('myTable');
$this->db->where('code','B');
$subQuery = $this->db->_compile_select();

$this->db->_reset_select();
// And now your main query
$this->db->select("*");
$this->db->where_in("$subQuery");
$this->db->where('code !=', 'B');
$this->db->get('myTable');

And the thing is done. Cheers!!!
Note : While using sub queries you must use

$this->db->from('myTable')

instead of

$this->db->get('myTable')

which runs the query.
Watch this too

How can I rewrite this SQL into CodeIgniter's Active Records?

Note : In Codeigntier 3 these functions are already public so you do not need to hack them.

Anatolic answered 14/6, 2012 at 10:44 Comment(0)
S
17
$data = $this->db->get_where('columnname',array('code' => 'B'));
$this->db->where_in('columnname',$data);
$this->db->where('code !=','B');
$query =  $this->db->get();
return $query->result_array();
Smocking answered 18/4, 2013 at 14:20 Comment(0)
H
7

Try this one:

$this->db->select("*");
$this->db->where_in("(SELECT trans_id FROM myTable WHERE code = 'B')");
$this->db->where('code !=', 'B');
$this->db->get('myTable');

Note: $this->db->select("*"); is optional when you are selecting all columns from table

Hearts answered 14/6, 2012 at 8:36 Comment(0)
B
1

try this:

return $this->db->query("
     SELECT * FROM myTable 
     WHERE trans_id IN ( SELECT trans_id FROM myTable WHERE code='B') 
     AND code!='B'
     ")->result_array();

Is not active record but is codeigniter's way http://codeigniter.com/user_guide/database/examples.html see Standard Query With Multiple Results (Array Version) section

Brashear answered 13/6, 2012 at 21:20 Comment(0)
N
0

you can use a simpler approach, while still using active record

$this->db->select("a.trans_id ,a.number, a.name, a.phone")
$this->db->from("Name_Of_Your_Table a");
$subQueryIn = "SELECT trans_id FROM Another_Table";
$this->db->where("a.trans_id in ($subQueryIn)",NULL);
Nard answered 14/10, 2021 at 6:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.