Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7940401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:22:43+00:00 2026-06-03T23:22:43+00:00

So I am trying to delete from multiple tables (in this scenario 6). I

  • 0

So I am trying to delete from multiple tables (in this scenario 6). I tried using INNER JOINS to delete rows from them all in one query, but couldn’t get it to work.

Here is my obnoxious work around:

        // 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);

How can I simplify this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:22:44+00:00Added an answer on June 3, 2026 at 11:22 pm

    You can simplify this bit:

    $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); 
    } 
    

    to simply
    $this->db->query(“DELETE FROM quiz_answers WHERE question_id = “.$q->id);

    The other option (if you have permission) is to set up foreign keys and “ON DELETE CASCADE”. http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

    Otherwise, without knowing the database structure it’s going to be difficult to receommend much more. You could use sub-selects but you’d still need some cascading.

    Another option would be to change the DB structure to include primary keys based off more than one field (e.g course_topic_dotpoints would be “topic_id, badge_id, dotpoint_id”) then you can delete more in one go – but this probably requires a fair bit of changing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I'm trying to delete multiple rows from a table and one of those
I trying to figure out how I can delete from multiple tables in SQL
im trying to do delete records from multiple database tables. For error handling i'm
I am trying to delete from multiple tables. Here's what my tables look like
I am trying to delete rows from datagridview, when the user selects any row
I am trying to delete a project from the projects table and all the
I am trying to delete all numbers from a string as long as the
I'm trying to use this query to delete the rows that are already on
While searching for clues on how i can delete multiple rows from QTableView i
I am trying to figure out how I can delete multiple records using check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.