Solved

Delete SQL intersection

Posted on 2008-10-24
1
1,130 Views
Last Modified: 2012-05-05
I have a MySQL database that I want to delete some rows in a table from based upon whether or not a column in the table that I want to delete rows from has a defined value in the cross referenced table.

To be precise, one table has this format:

Account Table:

        accnt_id
      accnt_email       varchar(50)
        .....
      accnt_date

and the other has this format.

Property Table:

        prty_id
      cty_id
      accnt_id
      prty_first_property
      ......
      prty_date

Both tables have a column for accnt_id.  I want to delete all rows from the Account table that do not have a row in the property table with accnt_id =  accnt_id.  Basically, if they don't have any properties with their account id in the properties table I want to delete that row in the accounts table.

SELECT * from `property` WHERE `accnt_id` = xx  pulls all rows with accnt_id == xx from the propery table.  I need to know how to use that through an intersection to delete matching rows in the accounts table.

A general reference for command line SQL manipulations would be appreciated.
0
Comment
Question by:sscotti
1 Comment
 
LVL 9

Accepted Solution

by:
jamesgu earned 350 total points
ID: 22801398
delete from Account  
where not exists ( select 1 from Property where Property.accnt_id =  Account.accnt_id)

0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now