Php codeigniter中的嵌套查询

Php codeigniter中的嵌套查询,php,mysql,codeigniter,Php,Mysql,Codeigniter,现在有些事情不对劲 $this->db->whereMark\u ID,从Mark中选择Mark\u ID,其中Course\u ID=$Course\u ID&&Matric\u No=$Matric\u No 在我的模型里?有什么建议吗?谢谢 function update_record($Course_ID,$Matric_No) { if($Course_ID && $Matric_No != NULL) {

现在有些事情不对劲

$this->db->whereMark\u ID,从Mark中选择Mark\u ID,其中Course\u ID=$Course\u ID&&Matric\u No=$Matric\u No

在我的模型里?有什么建议吗?谢谢

 function update_record($Course_ID,$Matric_No)
     {  
         if($Course_ID && $Matric_No != NULL)
         {
         $data = array(
             //'Course_ID' => $this->input->post('Course_ID'),
             'Matric_No' => $this->input->post('Matric_No'),
             'Student_Name' => $this->input->post('Student_Name'),
             'Result_Mark_1' => $this->input->post('Result_Mark_1'),
             'Result_Mark_2' => $this->input->post('Result_Mark_2'),
             'Result_Mark_3' => $this->input->post('Result_Mark_3'),
             'Result_Mark_4' => $this->input->post('Result_Mark_4'),
             'Result_Mark_5' => $this->input->post('Result_Mark_5')

                 );

           $this->db->where("Mark_ID,(SELECT Mark_ID FROM mark WHERE Course_ID=$Course_ID && Matric_No=$Matric_No)");

           $this->db->update('mark', $data);
         }
     }
你需要使用IN子句

但如果使用同一表中的子查询进行更新,则将面临以下错误:

无法在更新查询中指定目标表

为此,需要为子查询指定新的别名,如

$subquery="SELECT t.Mark_ID FROM(
           SELECT Mark_ID 
           FROM mark  
           WHERE Course_ID=$Course_ID && Matric_No=$Matric_No
           ) t ";

$this->db->where("Mark_ID IN($subquery)",null,FALSE);

我建议您通过忘记嵌套查询来简化您的生活。若你们仍然想使用嵌套查询,上面给出的答案是正确的。您需要在中使用


我想你可以使用下面的代码来得到你的结果

$this->db->selectMark\u id

$this->db->whereCourse\u ID,$Course\u ID

$get\u id=$this->db->get$this->tbl\u user->row

获得标记id后,只需将其传递给下面的查询

$this->db->where'Mark\u id',$get\u id->Mark\u id


$this->db->update$this->tbl_user,$data

为什么??您不必使用Mark_ID&因为您正在更新并从同一个表中选择,直接添加条件将达到相同的目的。谢谢您的回复。问题是我只有$Course\u ID和$Matric\u No,更新需要根据Mark\u ID进行。因此,我需要首先获取Mark\u ID。这根本没有意义,您根本不需要对它们进行子查询,因为它是同一个表&您有在正常where语句中使用的键。如果你直接使用它们,它会产生同样的效果。类似这样的效果应该可以:$this->db->whererray'Course\u ID'=>$Course\u ID',Matric\u No'=>$Matric\u No->update'mark',$data;对不起,我想得太多了。它很好用。非常感谢。是的,我以前用过这个方法,但是遇到了错误。我的方法是不要有$result=$a->row;并且您建议的方法面临的错误接近WHERE condition.fixed。我的错。那是个打字错误。
$subquery="SELECT t.Mark_ID FROM(
           SELECT Mark_ID 
           FROM mark  
           WHERE Course_ID=$Course_ID && Matric_No=$Matric_No
           ) t ";

$this->db->where("Mark_ID IN($subquery)",null,FALSE);
function update_record($Course_ID,$Matric_No)
     {  
         if($Course_ID && $Matric_No != NULL)
         {
         $data = array(
             //'Course_ID' => $this->input->post('Course_ID'),
             'Matric_No' => $this->input->post('Matric_No'),
             'Student_Name' => $this->input->post('Student_Name'),
             'Result_Mark_1' => $this->input->post('Result_Mark_1'),
             'Result_Mark_2' => $this->input->post('Result_Mark_2'),
             'Result_Mark_3' => $this->input->post('Result_Mark_3'),
             'Result_Mark_4' => $this->input->post('Result_Mark_4'),
             'Result_Mark_5' => $this->input->post('Result_Mark_5')

                 );

            // pre update query
            $this->db->select('Mark_id');
            $this->db->where('Course_ID', $Course_ID);
            $this->db->where('Matric_No', $Matric_No);
            $tmp_result = $this->db->get();
            $result = $tmp_result->row();
            $mark_id = $result->Mark_id;

            //updation
           $this->db->where("Mark_ID",$mark_id);
           $this->db->update('mark', $data);
         }
     }