所以我试图从多个表中删除(在这种情况下 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);
我怎样才能简化这个?