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 6187799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:07:11+00:00 2026-05-24T02:07:11+00:00

I have a few tables in MySQL like so: A —- A_has_B —- B

  • 0

I have a few tables in MySQL like so:

A ---- A_has_B ---- B
    (many to many)  |
                    |
C ---- C_has_B-------
    (many to many)

I need to delete rows from B given that:

  • The row to be deleted are referenced in C_has_B by C.C_ID
    • The row to be deleted are not referenced by other C.C_IDs in C_has_B
    • The row to be delete is also not referenced in A_has_B

I am able to get all the rows to be deleted referenced by C.ID using:

DELETE * FROM C
JOIN C_has_B
ON(C.C_ID = C_has_B.C_ID)
JOIN B
on(C_has_B.B_ID = B.B_ID)
where C.C_ID = 1;

I am not too sure how I can implement the last 2 conditions. Is it even possible to do this with just 1 query?

  • 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-05-24T02:07:12+00:00Added an answer on May 24, 2026 at 2:07 am

    You want to delete rows from B that are referenced only once in C_has_B and not referenced by A_has_B.

    With subqueries:

    DELETE B
    FROM
        B
    JOIN
        (
           -- get rows from C_has_B that reference B_ID only once
           SELECT B_ID, Count(*)
           FROM C_has_B
           GROUP BY B_ID
           HAVING Count(*) = 1
        )
        as C_has_B_once
        on (B.B_ID = C_has_B_once.B_ID)
    WHERE
        -- filter to rows not referenced by A_has_B
        B.B_ID not in (
           SELECT B_ID
           FROM A_has_B
        )
    

    Without subqueries:

    DELETE B
    FROM
        B
    -- get rows from C_has_B that reference B_ID only once
    -- (find 1 match via C_has_B1 and make sure there are no other C_IDs referencing the same B_ID via C_has_B2)
    JOIN
        C_has_B as C_has_B1
        on (B.B_ID = C_has_B1.B_ID) and (A_has_B.B_ID = NULL)
    LEFT JOIN
        C_has_B as C_has_B2
        on (C_has_B1.B_ID = C_has_B2.B_ID) and (C_has_B1.C_ID <> C_has_B2.C_ID)
    -- filter to rows not referenced by A_has_B
    LEFT JOIN
        A_has_B
        on (B.B_ID = A_has_B.B_ID) and (A_has_B.B_ID = NULL)
    WHERE
        -- exclude rows that join to C_has_B2
        (C_has_B2.C_ID is NULL)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few tables that I've defined like the below examples: class TableA
I have a few huge tables on a production SQL 2005 DB that need
I have a MySQL database that has a few very simple tables. I would
I have large MySQL tables with hundreds of thousands of rows. I need to
I have a few tables that have similar fields but not exactly the same.
I have a few tables with 70-80 columns in them. I would like to
I have a few HTML files that I'd like to include via tags in
I am trying to SELECT records from two MySql tables. I would like all
I have a small mySQL database and a few simple php based webpages that
I have a MySQL DB with a user that has access only a few

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.