【问题标题】:How to write a particular condition in brackets in active record codeigniter如何在活动记录codeigniter的括号中写入特定条件
【发布时间】:2017-12-20 12:31:44
【问题描述】:

我需要在活动记录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】:

我认为您的查询方法不好,而不是编码问题。主要查询在数据库中工作正常吗?用括号对any进行分组是没有意义的,结果是一样的。

你永远不会找到 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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多