Solved

DELETE ... WHERE NOT EXISTS problem

Posted on 2006-06-14
1
3,961 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
[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
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 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