在数据库php codeigniter中匹配逗号分隔值存储

时间:2014-12-06 06:33:01

标签: php mysql sql codeigniter

我有项目要求,如果有任何科目与学院添加的科目匹配,学生可以申请学位。 这是表结构

institute table                                 student table
----------------------------------            -----------------------------------
id | degree |       subject_required           id  |  subject_known
1  |   MS   |      maths,electronics,CAD        1    craft,drama 
2     BSC        chemistry,biology              2     maths
3     arts       craft,drama,dancing            3     cad,electronics

这是我写的代码,其中学院添加了学位和subject_required

public function add_degree($institute_id){
$data=array(
'id' => $institute_id,
'degree' => $this->input->post('degree'),
'subject_required'=>$this->input->post('subject_required'),
);
return $this->db->insert('institute',$data);
}

这个代码是在学生第一次注册时编写的

public function student_register(){
$data=array(
'id' => $institute_id,
'subject_known'=>$this->input->post('subject_known'),
);
return $this->db->insert('student',$data);
}

如何匹配此主题要求,如果任何一个科目匹配,那么学生可以申请并且该科目是按专栏分类

public function match_subject($id){
$sql='SELECT subject_required   FROM institute  WHERE id ='.$id;
$query=$this->db->query($sql);
foreach ($query->result() as $row)
     {
     $subject_required = $row->subject_required;
         $subject_required=$this->explode(array(',',' '),$subject_required);

     }

}

但我无法满足主题要求。

1 个答案:

答案 0 :(得分:1)

尝试使用MySQL Regex

select * from institute where subject_required REGEXP '[[:<:]]chemistry[[:>:]]'

我希望这会对你有所帮助