在codeigniter中使用Mysql WHERE IN子句 [英] Using Mysql WHERE IN clause in codeigniter

查看:695
本文介绍了在codeigniter中使用Mysql WHERE IN子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下mysql查询。

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的子查询方式来做到这一点,你将不得不hack 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')

$ b b

运行查询。

观看这个

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆