0

所以我试图从多个表中删除(在这种情况下 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

4 回答 4

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”),然后您可以一次性删除更多 - 但这可能需要相当多的更改.

于 2012-05-11T05:10:14.200 回答
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;
于 2012-05-11T05:26:34.607 回答
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'
                    );

这很完美

于 2012-05-11T06:42:11.153 回答
0

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

分隔符 $
CREATE TRIGGER Trigger_Name BEFORE DELETE ON some_table
FOR EACH ROW BEGIN DELETE FROM some_other_table WHERE some_id = NEW.id;
结束 $

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

Every time you execute a delete function on some_table, it will delete the row in some_other_table with the id you have.

read http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html for more information.

于 2012-05-11T14:51:32.343 回答