在 codeigniter 中使用 Mysql WHERE IN 子句 [英] Using Mysql WHERE IN clause in codeigniter
问题描述
我有以下 mysql 查询.你能告诉我如何用 Codeigniter 的方式编写相同的查询吗?
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'
推荐答案
您可以使用 codeigniter 的子查询方式来执行此操作,为此您必须破解 codeigniter.像这样
转到系统/数据库/DB_active_rec.php从这些函数中删除 public 或 protected 关键字
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')
代替
$this->db->get('myTable')
运行查询.
也看这个
which runs the query.
Watch this too
我该如何重写这个SQL 进入 CodeIgniter 的活动记录?
注意:在 Codeigntier 3 中,这些功能已经是公开的,所以你不需要破解它们.
Note : In Codeigntier 3 these functions are already public so you do not need to hack them.
这篇关于在 codeigniter 中使用 Mysql WHERE IN 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!