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

问题描述 投票:0回答:1

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

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');

上述查询的输出是:

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)
AND `question_id` = '4'
AND `answer_id` IN(12, 13)
AND `question_id` = '5'
AND `answer_id` IN(16, 17)
AND `question_id` = '6'
AND `answer_id` = 19
AND `question_id` = '7'
AND `answer_id` = 22
AND `question_id` = '8'
AND `answer_id` = 24
AND `question_id` = '9'
AND `answer_id` IN(26, 27)
codeigniter
1个回答
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))
© www.soinside.com 2019 - 2024. All rights reserved.