匹配以逗号分隔的主题来自两个不同的表

时间:2014-12-08 05:27:42

标签: php mysql sql codeigniter

我有条件需要匹配学生所知的科目和学院发布的posts_subjets,这个科目用逗号分隔 .i已编写以下代码,其工作正常,但看起来很长..
谁能告诉我另一种方法呢。
   以下是表结构

posted_subjets table                                                            student table
-----------------------------------------------------                           -----------------------------------    
posted_subjets_id  | degree  | subject_required |     institute_id               student_id  |  subjects
1                  |   MS   |  maths,electronics,CAD     2                         1           craft,drama 
2                    BSC       chemistry,biology         3                         2           maths
3                    arts      craft,drama,dancing        1                        3           cad,electronics

以下是工作代码

public function match_subjects($sub,$sub_id)
        {
            $student_id=$this->session->userdata('student_id');
            $flag = False;

            $y=0;
            while($y<count($sub))
            {
                $sql="SELECT subjects 
                      FROM student
                      WHERE subjetcs
                      LIKE '%$sub[$y]%' 
                      AND student_id = '$student_id'";

                $query=$this->db->query($sql);

                if(count($query->result()))
                {   
                    $flag = TRUE;
                }
                $y++;
            }

            if($flag)
            {   
                $sql='SELECT subjets      
                      FROM posted_subjets
                      WHERE posted_subjets_id ='.$sub_id;

                $query=$this->db->query($sql);

                $b=array();

                foreach ($query->result() as $row)
                {
                    $subjets       = $row->subjets      ;

                }

                $subjets      =$this->multiexplode(array(',',' '),$subjets      );

                $flag=false;
                $x=0;

                for($i=0;$i<count($sub);$i++)
                {
                echo $sub;
                    for($k=0;$k<count($subjets );$k++)
                    {   
                        $subjets      [$k] = strtolower($subjets      [$k]);
                        $sub[$i] = strtolower($sub[$i]);
                        if($subjets      [$k]==$sub[$i])
                        {
                            $flag=true;
                            break;
                        }
                    }
                }
                if($flag)
                {
                    while($x<count($subjets))
                    {
                        $sql="SELECT subjects
                              FROM student
                              WHERE subjects LIKE '%$subjets[$x]%' 
                              AND student_id = '$student_id'";
                        $query=$this->db->query($sql);

                        if(count($query->result()))
                        {   
                            $reg_user_id=$this->session->userdata('student_id');

                            $query=$this->db->query("SELECT institute_id 
                                                     FROM posted_subjets
                                                     WHERE posted_subjets_id=".$sub_id
                                                     );

                            foreach($query->result() as $row)
                            {
                                $eid=$row->employer_id;
                            }
                            $this->db->query("INSERT INTO applied_subject(posted_subjets_id,institute_id,student_id,action)
                                              VALUES (".$job_id.",'$eid','$student_id','Applied')"
                                              );

                            $this->db->query("UPDATE posted_subjetcs 
                                              SET application_count=application_count+1 
                                              WHERE posted_subjets_id=".$sub_id
                                            );
                            return true;
                        }
                        $x++;
                    }
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }                                    

如果有人能告诉我另一种方法。

1 个答案:

答案 0 :(得分:0)

正如我评论的那样,我会将DB结构更改为:

posted_sujects表

posted_subjets_id  | degree  | institute_id 
1                     MS        1         
2                    BSC        3
3                    arts       1

学生表

student_id  |  other fields
1              Boby
2              Roger

科目表

subject_id | name
1            maths
2            electronics
3            chemistry

student_subject表

subject_id | student_id
1            1
2            1
1            2

post_subject表

subject_id | post_id
1            1
2            1

基本上,您可以使用post_subject加入student_subject,以匹配所需的科目和学生。这是一个快速查询(未测试)

SELECT * FROM student_subject
INNER JOIN post_subject ON student_subject.subject_id = post_subject.subject_id

然后从那里你可以添加GROUP BY子句来按帖子分组。