?
Solved

SQL deLETE QUERY

Posted on 2011-03-19
4
Medium Priority
?
168 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

840 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