如何比较一个表与另一个表的ID

时间:2017-12-20 14:46:41

标签: mysql codeigniter

我有两个表tb_user_answers和tb_preference_dropdown_answers如下:

tb_preference_dropdown_answers

user_id  question_id  answer_id
1          1             2
1          2             1
1          3             1
1          4             1
1          5             1
1          6             0
1          7             0
1          8             1
1          9             1

tb_user_answers

user_id   question_id    answer_id
2             1             2
2             2             6
2             3             9
2             4            13
2             5            16
2             6            19
2             7            22
2             8            24
2             9            26
3             1             2
3             2             5
3             3             8
3             4            13
3             5            16
3             6            19
3             7            22
3             8            24
3             9            27

我要做的是从tb_preference_dropdown_answers获取与特定user_id相对应的记录,并将其与tb_user_answers表匹配,并且所有相应的记录应匹配。我们还需要从tb_user_answers中获取与首选项表的记录匹配的user_id,条件包括以下条件,包括我在查询中使用的条件,但查询未获取记录:

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

0 个答案:

没有答案
相关问题