Jump to content
  • Advertisement
Sign in to follow this  
chbrules

[SQL] Deleting Records While Comparing

This topic is 4829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I have 2 tables with records, each has a unique ID. I want to compare the ID's from table 1 to table 2. If The ID exists on table 2, delete the record from table 1. Doesn't sound like too much right? I can't seem to figure it out for the life of me though. Does anyone know how I can do this? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I'm not quite sure about this, but I think it's worth a try:

DELETE table1 FROM table1, table2 WHERE table1.id = table2.id;

Greets, aco

Share this post


Link to post
Share on other sites
No it won't allow me to list multiple tables in the DELETE FROM. BTW this is MS SQL if that makes any difference to what functions I have access to accomplish this.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ah, ok. This was from the MySQL manual. I don't know about MS SQL...

"Brute force" solution:

SELECT ID FROM table1;

DELETE FROM table2 WHERE ID = x1 OR ID = x2 OR ... ;

You can build that 2nd statement from the result of the first one.

Greets, aco

(sorry for any bad english)

Share this post


Link to post
Share on other sites
This works for me on SQL Server 2000

delete from table1 where ID in (Select t1.ID from table1 t1, table2 t2 where t1.ID = t2.ID)

Basically it says delete any rows from table1 where the ID number is in the result set created by the select query

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!