How do I write this MySQL query for Codeigniter?
SELECT *
FROM myTable
WHERE trans_id IN (
SELECT trans_id FROM myTable WHERE code='B'
)
AND code!='B'
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.
where_in()? Only passing one parameter to where_in() will result in a NULL second parameter. When either the first or second parameter of a where_in() (which calls _where_in() internally) is NULL, then the method does an early return without adding the WHERE IN expression at all. My suspicion is that developers who have used this advice have implemented a silently inaccurate query.$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();
where_in() (which typically expects a flat array).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
where_in() will result in a NULL second parameter. When either the first or second parameter of a where_in() (which calls _where_in() internally) is NULL, then the method does an early return without adding the WHERE IN expression at all.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
For most RDBMSs, using a JOIN will outperform performing a subquery for each potentially qualifying row.
SELECT a.*
FROM myTable a
JOIN myTable b USING (trans_id)
WHERE a.code != 'B' AND b.code = 'B'
CodeIgniter:
return $this->db
->select('a.*')
->join('myTable b', 'trans_id')
->get_where(
'myTable b',
[
'a.code !=' => 'B',
'b.code' => 'B'
]
)
->result();
If your script cannot be refactored to use a JOIN (maybe you are using [WHERE IN subquery] as part of an UPDATE query and your database dialect complicates that), you can use where_in() with a pre-compile subquery if you disable the auto-quoting in where_in().
$sub = $this->db
->select('trans_id')
->where('code', 'B')
->get_compiled_select('myTable');
return $this->db
->where_in('trans_id', $sub, false)
->get_where('myTable', ['code !=' => 'B'])
->result();
Rendered SQL:
SELECT *
FROM `myTable`
WHERE trans_id IN(SELECT `trans_id` FROM `myTable` WHERE `code` = 'B')
AND `code` != 'B'
Technically, you can avoid the temporary subquery variable declaration if it is compiled before any part of the parent query is built (same result).
return $this->db
->where_in(
'trans_id',
$this->db
->select('trans_id')
->where('code', 'B')
->get_compiled_select('myTable'),
false
)
->get_where('myTable', ['code !=' => 'B'])
->result();
codehave? Would a GROUP BY and HAVING possible work for your intention so that you don't need any subqueries?