Solved

how to delete records that contain no child records

Posted on 2011-02-25
5
280 Views
Last Modified: 2012-06-27
I need to delete all order records that do not container order items.

the tables are linked by the orderId on the orderItems table. So i can do the following

select * from orders o
inner join orderitems oi on o.id = oi.orderid

How can i detect which order records contain no child orderItems
0
Comment
Question by:frosty1
5 Comments
 
LVL 13

Expert Comment

by:devlab2012
ID: 34981543
delete from orders o where not exists(select * from orderitems oi where oi.orderid = o.orderid)
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34981566
backup your db before tyr
delete from orders where id not in
 (select orderid from orderitems )

Open in new window

0
 
LVL 3

Accepted Solution

by:
jmro20 earned 500 total points
ID: 34981586
Delete From Orders
          Left Join OrderItems On Orders.OrderId = OrderItems.OrderId
Where OrderItems.OrderId IS NULL      
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 34982526
I would recommend using jmro20's answer.  Among other points in its favor is the fact that it is probably going to provide the best performance.
0
 
LVL 3

Expert Comment

by:jmro20
ID: 34997724
Did you solved your problem?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

21 Experts available now in Live!

Get 1:1 Help Now