在气体ORM中查询Mysql

时间:2015-03-03 11:27:00

标签: mysql codeigniter

  1. 这是我要来的

    SELECT * FROM (`lkt_messages`) WHERE `sender_id` = '1' AND `receiver_id` = '2' OR `sender_id` = '2' OR `receiver_id` = '1' ORDER BY `added_on` DESC 
    
  2. 我想要这样

        SELECT * FROM (`lkt_messages`) WHERE `sender_id` = '1' AND `receiver_id` = '2' OR `sender_id` = '2' AND `receiver_id` = '1' ORDER BY `added_on` DESC 
    

    我在下面写下了这个问题。

        $cls=MESSAGES_TBL;
                $id= $this->session->userdata('user_id');
                $condition_query=array('sender_id'=>$replymsg_id,'receiver_id'=>$id);
                $condition_query1=array('sender_id'=>$id,'receiver_id'=>$replymsg_id);
    
                $data=$cls::where($condition_query)->or_where($condition_query1);
    
                 $result=$data->order_by('added_on', 'DESC')->all();
    
                return $result; 
    

2 个答案:

答案 0 :(得分:1)

以下更改应该有效:

$condition_query = "(sender_id = $replymsg_id and receiver_id = $id)";

$condition_query1 = "(sender_id = $id and receiver_id = $replymsg_id)";

$data = $cls::where($condition_query)->or_where($condition_query1);

答案 1 :(得分:0)

试试这个SQL查询: -

SELECT * 
FROM (`lkt_messages`) 
WHERE (`sender_id` = '1' AND `receiver_id` = '2') 
OR (`sender_id = '2' AND `receiver_id` = '1') 
ORDER BY `added_on` DESC