Solved

Delete SQL intersection

Posted on 2008-10-24
1
1,164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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