Solved

how to delete records that contain no child records

Posted on 2011-02-25
5
293 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
[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
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

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

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