匹配逗号分隔值存储在数据库中的PHP笨(match Comma Separated value st

2019-10-21 08:43发布

我有项目需求,其中学生可以申请到的程度,如果有任何学科与学科由机构增加相匹配。 这里是表结构

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

我如何匹配,如果任何一个主题,然后匹配,其中学生可以申请这一主题要求,本科目按列separed

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

     }

}

但林不能够匹配的主题要求。

Answer 1:

尝试与MySQL正则表达式

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

我希望这能帮到您



文章来源: match Comma Separated value store in database php codeigniter