Solved

DELETE ... WHERE NOT EXISTS problem

Posted on 2006-06-14
1
3,929 Views
Last Modified: 2008-01-16
Hi Experts,

I'm currently using MySQL 4.1.11.  I'm just wondering why I couldn't run the query below :
    -------------------------------------
    DELETE FROM `product` AS p
    WHERE NOT EXISTS (
        SELECT * FROM `category`
        WHERE `id` = p.`fk_cat_id`
    )
    -------------------------------------

But using SELECT (it will work fine...):
    -------------------------------------
    SELECT * FROM `product` AS p
    WHERE NOT EXISTS (
        SELECT * FROM `category`
        WHERE `id` = p.`fk_cat_id`
    )
    -------------------------------------

Actually, I have tried running both queries in Sybase without any problem.

So, right now I'm just using another query just to meet the expected result.  And here's the query :
    -------------------------------------
    DELETE FROM `product`
    WHERE `fk_cat_id` NOT IN (
        SELECT `id` FROM `category`
    )
    -------------------------------------

Any idea on how to create a query using EXISTS instead of IN ???  Or, any recommendation as long as performance is concern.

Any help is greatly appreciated...


Thanks!
eNTRANCE2002 :-)
0
Comment
Question by:Renante Entera
1 Comment
 
LVL 5

Accepted Solution

by:
Z03niE earned 500 total points
ID: 16901046
How about :

DELETE `product` FROM `product`
left join `category`  on `product`.`fk_cat_id` = `category`.`id`
WHERE `category`.`id` is null
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

9 Experts available now in Live!

Get 1:1 Help Now