Solved

Delete SQL intersection

Posted on 2008-10-24
1
1,157 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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