从多个表中删除

时间:2012-05-11 04:50:58

标签: php mysql codeigniter

所以我试图从多个表中删除(在这个场景6中)。我尝试使用INNER JOINS在一个查询中删除所有行,但无法使其工作。

这是我讨厌的工作:

        // Delete course and everything linked to it (topics, badges, dotpoints, questions & answers)   
        $topic = $this->db->query("SELECT * FROM course_topics WHERE course_id = ".$row_id);    
        foreach ($topic->result() as $t)
        {
            $badge = $this->db->query("SELECT * FROM course_topic_badges WHERE topic_id = ".$t->id);    
            foreach ($badge->result() as $b)
            {
                $dotpoint = $this->db->query("SELECT * FROM course_topic_dotpoints WHERE badge_id = ".$row_id);
                foreach ($dotpoint->result() as $d)
                {
                    $question = $this->db->query("SELECT * FROM quiz_questions WHERE dotpoint_id = ".$d->id);
                    foreach ($question->result() as $q)
                    {
                        $answer = $this->db->query("SELECT * FROM quiz_answers WHERE question_id = ".$q->id);
                        foreach ($answer as $a)
                        {
                            $this->db->query("DELETE FROM quiz_answers WHERE question_id = ".$q->id);
                        }
                        $this->db->query("DELETE FROM quiz_questions WHERE dotpoint_id = ".$d->id);
                    }
                    $this->db->query("DELETE FROM course_topic_dotpoints WHERE badge_id = ".$b->id);
                }
                $query = $this->db->query("DELETE FROM course_topic_badges WHERE topic_id = ".$t->id);
            }
            $query = $this->db->query("DELETE FROM course_topics WHERE course_id = ".$row_id);
        }
        $query = $this->db->query("DELETE FROM courses WHERE id = ".$row_id);

我该如何简化?

4 个答案:

答案 0 :(得分:0)

您可以简化此位:

$answer = $this->db->query("SELECT * FROM quiz_answers WHERE question_id = ".$q->id); 
foreach ($answer as $a) 
{ 
    $this->db->query("DELETE FROM quiz_answers WHERE question_id = ".$q->id); 
} 

简单地说     $ this-> db-> query(“DELETE FROM quiz_answers WHERE question_id =”。$ q-> id);

另一个选项(如果您有权限)是设置外键和“ON DELETE CASCADE”。 http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

否则,在不知道数据库结构的情况下,很难再进一步推荐。您可以使用子选择,但仍需要一些级联。

另一个选择是更改数据库结构以包含基于多个字段的主键(例如,course_topic_dotpoints将是“topic_id,badge_id,dotpoint_id”)然后您可以一次删除更多 - 但这可能需要公平有点变化。

答案 1 :(得分:0)

you need to SET FOREIGN_KEY_CHECKS = 0;
you can read more dev.mysql.com/doc/en/innodb-foreign-key-constraints.html
SET @BACKUP_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS; 
SET @@FOREIGN_KEY_CHECKS=0;

-- Do stuff here

SET @@FOREIGN_KEY_CHECKS=@BACKUP_FOREIGN_KEY_CHECKS;
SET @BACKUP_FOREIGN_KEY_CHECKS=NULL;

答案 2 :(得分:0)

我已经为自己创建了一个将从多个表中删除的函数 这是它

    function delete_multiple_rows($table    ,   $condition  ,   $join_condition)
    {
        $table_count    =   count($table);
        $join_count     =   count($join_condition) + 1; 

        if($table_count ==  $join_count)
        {
            if(count($condition)>0)
            {
                $delete_query    =  'DELETE '.  implode(' , ',$table)   .' FROM '.$table[0];

                $counter    =   1;
                foreach($join_condition as $key => $value)
                {
                    $delete_query   .=  ' LEFT JOIN '   .   $table[$counter]    .   ' ON '  .   $key    .   ' = '   .   $value;
                    $counter++;
                }

                $condition_counter  =   1; 

                foreach($condition as $key  => $value)
                {
                    if($condition_counter > 1)
                    {
                        $delete_query   .=  ' AND ';
                    }else{
                        $delete_query   .=  ' WHERE ';
                    }
                    $delete_query   .=  $key    .   ' = '   .   $value;
                    $condition_counter++; 
                }           
                $delete_query;              
                $this->db->query($delete_query);

            }else{
                echo 'This query requires at least 1 where condition.';
            }       
        }else{

            $table_count    =   count($table)   -   1;
            echo 'At least '    .   $table_count    .   ' join conditions must be provided.';
        }
    } 

如何使用

这些是控制器方法参数

$table  =   array('table1','table2','table3');
$condition  =   array('id'=>1,'2nd_condition'=>'blah');
$join_condition =   array(
                    'table1.column' =>  'table2.column',
                    'table1.column' =>  'table3.column'
                    );

这很完美

答案 3 :(得分:0)

您可能应该在mysql数据库中设置一些触发器,而不是处理发送查询。

  

分隔符$
  在删除some_table之前创建TRIGGER Trigger_Name   为了每行开始       删除some_other_table WHERE some_id = NEW.id;
  结束$

将它放在PHPMYADMIN的SQL中,它将创建一个名为Trigger_Name的触发器(并不重要)。

每次在some_table上执行删除功能时,它都会删除some_other_table中包含你的id的行。

阅读http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html了解详情。