4

我得到array()一个人就读的学校。对于用户sarah@gmail.com,数组中有degree她获得的 3 秒。我的桌子只有她的 3 度中的 2 度。

插入表中尚不存在的莎拉学位的最简单方法是什么?

开始表:

email            school    degree 

d@d.com          Yale      BA
d@d.com          Emery     PHD
a@a.com          ClownU    BS
sarah@gmail.com  Harvard   BA
sarah@gmail.com  Harvard   Masters

茶几:

email            school    degree 

d@d.com          Yale      BA
d@d.com          Emery     PHD
a@a.com          ClownU    BS
sarah@gmail.com  Harvard   BA
sarah@gmail.com  Harvard   Masters
sarah@gmail.com  Harvard   PHD

我考虑过的选项:

  1. 删除 sarah 的所有记录。重新插入 sarah 的所有记录。
  2. 对于每条记录,查看它是否存在。如果是,什么也不做,否则写记录。
  3. 如果特定记录不存在,一些神奇的 CI 函数会更新_batch 或 inserts_batch?
4

3 回答 3

1
Insert Into [Start-Table] (email, school, degree)
(
select b.email, b.school, b.degree from [End-Table] b 
where NOT EXISTS (Select email, school, degree from [Start-Table]
                  where email = b.email and school = b.school and degree = b.degree)
)
于 2014-03-09T20:39:12.253 回答
0

如果为您的记录保留相同的 ID 并不重要,只需删除并重新插入所有数据即可。如果您想为旧记录保留相同的 ID,您也可以将其删除,但您首先要查询数据库以获取 ID 并使用它们创建一个数组。然后您可以插入所有新数据。

但如果我是你,我会创建一个函数来查找数据库以查找现有记录。如果记录存在,它会更新它。如果没有,它会创建一个新的。我认为这是解决这个问题的最干净的方法。

于 2013-08-12T08:33:37.337 回答
0

我遇到了同样的问题,有些像你,我像这样解决了它:1-删除你想要的用户的所有记录,然后像代码一样插入批处理。

           $employer_office        = $this->input->post('employer_office');
            $emp_job_type           = $this->input->post('job_type');
            $job_title              = $this->input->post('job_title');
            $job_appointment_date   = $this->input->post('job_appointment_date');
            $job_duration           = $this->input->post('job_duration');
            $job_place              = $this->input->post('job_place');
            $type_of_relation       = $this->input->post('type_of_relation');
            $monthly_salary         = $this->input->post('monthly_salary');
            $remarks                = $this->input->post('remarks');

            $all_array = array();

            if($employer_office)
            {
                if(is_array($employer_office))
                {

                   for($j=0; $j<count($employer_office); $j++)
                   {


                       $form_arr   = array(
                                    'employer_office'      =>$employer_office[$j],
                                    'job_type'             =>$emp_job_type[$j],
                                    'job_title'            =>$job_title[$j],
                                    'job_appointment_date' =>change_datei($job_appointment_date[$j]),
                                    'job_duration'         =>$job_duration[$j],
                                    'job_place'            =>$job_place[$j],
                                    'type_of_relation'     =>$type_of_relation[$j],
                                    'monthly_salary'       =>$monthly_salary[$j],
                                    'remarks'              =>$remarks[$j],
                                    'regdate'              =>date('Y-m-d H:m:s'),
                                    'userid'               =>$id
                                    );
                                    array_push($all_array, $form_arr);
                   }



                }
            }

            if($this->history_model->delete_all('ast_jobs_history',  $id)==TRUE)
            {
                if($this->history_model->all_insert('ast_jobs_history', $all_array)==TRUE)
                {
                    $this->session->set_flashdata("msg","<span class='m_success'>".$this->lang->line('global_update_success')."</span>");
                    redirect('history/home/list_dy','refresh');
                }
                else
                {
                    $this->session->set_flashdata("msg","<span class='m_error'>".$this->lang->line('global_update_error')."</span>");
                    redirect('history/home/list_dy','refresh');
                }
            }
            else
            {
                $this->load->view('unauthorized');
            }

并在模型 insert_batch 内。

        function all_insert($tbl, $data=array())
        {
            if(is_array($data))
            {
                $this->db->trans_start();
                $this->db->insert_batch($tbl,$data);
                $this->db->trans_complete();
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

带来一些改变,我希望这会给你一个关于如何解决你的问题的提示。

或者如果您不想删除特定ID的记录,也可以进行查询,根据ID获取数据并使用array_search()或in_array()函数过滤您发布的数据并获取您要插入的记录它,而不是将其插入数据库。

于 2013-08-12T05:49:57.567 回答