[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 600
  • Last Modified:

Delete Orphan Records

I have a couple of tables, one with Sources and one with Articles. I link the articles back to sources with a Source ID column in the Articles table.

What I'm wondering is if theres a simple way to delete all Articles records where the Source record it has in the Source ID column does not exist in the Sources table.

It seems like I should be able to do this with a single SQL statement, but I haven't been about to find how to do it. Right now I'm doing crazy loops through thousands of records to check for matches and delete ones that don't match.

Any suggestions?
0
cnxmax
Asked:
cnxmax
2 Solutions
 
Rajkumar GsSoftware EngineerCommented:
Try

DELETE FROM Articles WHERE SourceID NOT IN (SELECT SourceID FROM Sources)

Raj
0
 
the_billCommented:
DELETE Articles WHERE SourceID NOT IN (SELECT SourceID FROM Sources)
0
 
cnxmaxAuthor Commented:
Thanks for the quick response!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now