如何在活动记录codeigniter中的括号中写入特定条件

时间:2017-12-20 12:31:45

标签: codeigniter

我需要在活动记录codeigniter中转换mysql查询

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;

public class MCVE {
    public static void main(final String[] args) throws IOException {
        final Connection.Response response = Jsoup.connect("https://rss.packetstormsecurity.com/files/page18").execute();

        // Well formed XML
        System.out.println(response.body());

        // Malformed XML
        System.out.println(response.parse());
    }
}

我尝试但是无法使用以下代码执行此操作

SELECT `user_id` 
FROM `tb_user_answers` 
WHERE (`question_id` = '1' AND `answer_id` = '2') 
AND  (`question_id` = '2'  AND `answer_id` IN (4, 6)) 
AND (`question_id` = '3' AND `answer_id` IN (8,9,10))

上述查询的输出是:

$this->db->select('user_id');
foreach($user_preference_dropdown as $dropdown){
  if($dropdown['question_id'] == 1 && $dropdown['answer_id'] != 0){
    $this->db->where('question_id',$dropdown['question_id']);
    $this->db->where('answer_id', $dropdown['answer_id']);
  }
}
foreach($user_preference_dropdown as $dropdown){
  if($dropdown['question_id'] == 2 && $dropdown['answer_id'] == 1){
    $this->db->where('question_id',$dropdown['question_id']);
    $this->db->where_in('answer_id',[4,6]);
  }
}
foreach($user_preference_dropdown as $dropdown){
  if($dropdown['question_id'] == 3 && $dropdown['answer_id'] == 1){
    $this->db->where('question_id',$dropdown['question_id']);
    $this->db->where_in('answer_id',[8,9,10]);
  }
}
foreach($user_preference_dropdown as $dropdown){
  if($dropdown['question_id'] == 4 && $dropdown['answer_id'] == 1){
    $this->db->where('question_id',$dropdown['question_id']);
    $this->db->where_in('answer_id',[12,13]);
  }
}
foreach($user_preference_dropdown as $dropdown){
  if($dropdown['question_id'] == 5 && $dropdown['answer_id'] == 1){
    $this->db->where('question_id',$dropdown['question_id']);
    $this->db->where_in('answer_id',[16,17]);
  }
}
foreach($user_preference_dropdown as $dropdown){
  if($dropdown['question_id'] == 6 && $dropdown['answer_id'] == 0){
    $this->db->where('question_id',$dropdown['question_id']);
    $this->db->where('answer_id',19);
  }
}
foreach($user_preference_dropdown as $dropdown){
  if($dropdown['question_id'] == 7 && $dropdown['answer_id'] == 0){
    $this->db->where('question_id',$dropdown['question_id']);
    $this->db->where('answer_id',22);
  }
}
foreach($user_preference_dropdown as $dropdown){
  if($dropdown['question_id'] == 8 && $dropdown['answer_id'] == 1){
    $this->db->where('question_id',$dropdown['question_id']);
    $this->db->where('answer_id',24);
  }
}
foreach($user_preference_dropdown as $dropdown){
  if($dropdown['question_id'] == 9 && $dropdown['answer_id'] == 1){
    $this->db->where('question_id',$dropdown['question_id']);
    $this->db->where_in('answer_id',[26,27]);
  }
}
$query = $this->db->get('tb_user_answers');

1 个答案:

答案 0 :(得分:0)

我认为你的查询方法不好,而不是编码问题。主查询在数据库中是否正常工作?使用括号将任何分组都没有意义,结果是相同的。

你永远不会找到一个结果,其中question_id为1而且question_id为2,

我认为您的主要查询是:

SELECT `user_id` 
FROM `tb_user_answers` 
WHERE (`question_id` = '1' AND `answer_id` = '2') 
OR  (`question_id` = '2'  AND `answer_id` IN (4, 6)) 
OR (`question_id` = '3' AND `answer_id` IN (8,9,10))