Solved

SQL deLETE QUERY

Posted on 2011-03-19
4
159 Views
Last Modified: 2012-05-11
This:

SELECT * FROM Product_Categories WHERE Product_ID = '101'

Returns me 2 items one where Category_ID = 0 and another where it is something different then 0

How would I delete all items in the table where  Category_ID = 0 IF there is at least one OTHER item where  Category_ID is NOT 0
0
Comment
Question by:vbnetcoder
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 35172006
-- Run this as a SELECT first to make sure it only affects the rows you want to delete.  I also suggest you backup this table first.

select Product_ID
-- delete
from Product_Categories
where Category_ID = 0
  and Product_ID in (
         select Product_ID
         from Product_Categories
         group by Product_ID
         having count(*) > 1
  )

0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35172009
you can change the top line to SELECT * instead of SELECT Product_ID
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35172017
Delete from FROM Product_Categories where (Category_ID = 0) and (select count(Category_ID) from Product_Categories where Category_ID <> 0) > 1
0
 

Author Closing Comment

by:vbnetcoder
ID: 35172044
ty
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
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.

707 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

12 Experts available now in Live!

Get 1:1 Help Now