Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DELETE ... WHERE NOT EXISTS problem

Posted on 2006-06-14
1
Medium Priority
?
4,025 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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.
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

604 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