codeIgniter - 分组where子句 [英] CodeIgniter - Grouping where clause

查看:385
本文介绍了codeIgniter - 分组where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有codeIgniter这下面的查询:

I have this following query for CodeIgniter:

$q = $this->db->where('(message_from="'.$user_id.'" AND message_to="'.$this->auth_model->userdata['user_id'].'")')
            ->or_where('(message_from="'.$this->auth_model->userdata['user_id'].'" AND message_to="'.$user_id.'")')
            ->get('messages');

我想写这个查询具有完全的活动记录。

I want to write this query with completely active record.

我已经试过这样的事情:

I have tried something like this:

$from_where = array('message_from'=>$user_id, 'message_to'=>$this->auth_model->userdata['user_id']);
            $to_where   = array('message_from'=>$this->auth_model->userdata['user_id'],'message_to'=>$user_id);    
            $q = $this->db->where($from_where)
            ->or_where($to_where)
            ->get('messages');

            die($this->db->last_query());

以上code产生的查询:

The above code produces this query:

SELECT * FROM (`messages`) WHERE `message_from` = '2' AND `message_to` = '1' OR `message_from` = '1' OR `message_to` = '2'

但是,这就是我要生产:

But this is what I want to produce:

SELECT * FROM (`messages`) WHERE (message_from="2" AND message_to="1") OR (message_from="1" AND message_to="2")

有类似的问题这里和<一href="http://stackoverflow.com/questions/8748140/$c$cigniter-active-record-where-or-grouping">here,但thosedid不是为我提供了一个真正的解决方案。

There are similar questions here and here, but thosedid not provide a real solution for me.

这个怎么可能,通过核心库。如果不是,是否有一个扩展,它允许写这样的质疑?

How's this possible, If not via core libraries, is there an extension which allows writing such queries?

谢谢

推荐答案

您可以用codeigniter子查询的方式来做到这一点为这个目的,你必须破解codeigniter。喜欢这个 进入系统/数据库/ DB_active_rec.php从这些功能中删除公共或受保护的关键字

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->where('message_from','2');
$this->db->where('message_to','1');

$subQuery1 = $this->db->_compile_select();
$this->db->_reset_select();


$this->db->where('message_from','1');
$this->db->where('message_to','2');

$subQuery2 = $this->db->_compile_select();
$this->db->_reset_select();

$this->db->select('*');
$this->db->where("$subQuery1");
$this->db->or_where("$subQuery2");
$this->db->get('messages');

看我这个答案。这显示了如何使用子查询。这将有助于
<一href="http://stackoverflow.com/questions/11023318/using-mysql-where-in-clause-in-$c$cigniter/11031431#11031431">Using Mysql的凡在codeigniter条款

Look at this answer of mine. This shows how to use sub queries. This will help
Using Mysql WHERE IN clause in codeigniter

EDITES

是的,我已经做到了 改写这种方式正是您想要查询

Yes i have done it Rewrite the query this way exactly you want

$this->db->where('message_from','2');
$this->db->where('message_to','1');

$subQuery1 = $this->db->_compile_select(TRUE);
$this->db->_reset_select();


$this->db->where('message_from','1');
$this->db->where('message_to','2');

$subQuery2 = $this->db->_compile_select(TRUE);
$this->db->_reset_select();

$this->db->select('*');
$this->db->where("($subQuery1)");
$this->db->or_where("($subQuery2)");
$this->db->get('messages');

编译选择是与真正的参数。不会产生SELECT子句。这将产生

Compile select is with true parameter. Will not produce select clause. This will produce

SELECT * FROM (`messages`) WHERE (`message_from` = '2' AND `message_to` = '1') OR (`message_from` = '1' AND `message_to` = '2')

这篇关于codeIgniter - 分组where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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