Solved

SQL Syntax - Delete Records based on another tables data

Posted on 2010-09-08
3
282 Views
Last Modified: 2012-05-10
Hi

I have the following 2 tables:

tblProducts
Columns: ProductType, ReleaseDate, CustomerType

tblDormantProducts
Columns: ProductType, DateFrom, DateTo, CustomerType

How could I create an SQL DELETE statement to delete records from tblProducts based on records that match within tblDormantProducts?...

with tblProducts.ReleaseDate, this needs to match between tblDormantProducts.DateFrom and tblDormantProducts.DateTo.

Many thanks,

Rit
0
Comment
Question by:rito1
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 33628560
DELETE FROM tblProducts
FROM tblProducts p INNER JOIN
    tblDormantProducts d ON p.ProductType = d.ProductType
WHERE p.ReleaseDate BETWEEN d.DateFrom AND d.DateTo
0
 
LVL 30

Expert Comment

by:hnasr
ID: 33631808
Ceck this replace with your table and field names.
Table A (a, x)
Table B (a, b, x1, x2)

DELETE  * FROM A
WHERE A.a
IN (SELECT  A.a
FROM A INNER JOIN B ON A.a = B.a
WHERE A.x Between B.x1 AND B.x2)
0
 
LVL 1

Author Closing Comment

by:rito1
ID: 33637235
Thanks both but I went with matthewspatrick purely because I set to work on his syntax and it all made sense to me as I was implementing.

many thanks,

Rit
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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