Solved

Need help with a MySQL Query

Posted on 2011-02-19
3
433 Views
Last Modified: 2012-06-21
Hi,

in my Database I have a table called images. There I have two important columns: id and org_image_id. If a user modify an "original" image, the modified image is stored in the same table but with org_image_id = id of the original image.

What I want to do is to remove all modified images from the database, if there are no original image in the database. I wrote this query, but it does not work for me:

[SQL] delete FROM images WHERE org_image_id NOT IN(SELECT DISTINCT id FROM images WHERE org_image_id is NULL)and LRU < DATE_SUB(NOW(), INTERVAL 30 DAY);
[Err] 1093 - You can't specify target table 'images' for update in FROM clause

Open in new window


Can someone help me please?
0
Comment
Question by:LL0rd
[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
  • 2
3 Comments
 
LVL 26

Assisted Solution

by:tigin44
tigin44 earned 50 total points
ID: 34936080
As I understand this is not the whole query...This one relates to another.. if so could you provide the whole set...

delete FROM images
WHERE org_image_id NOT IN(SELECT DISTINCT id FROM images WHERE org_image_id is NULL)
     and LRU < DATE_SUB(NOW(), INTERVAL 30 DAY);
0
 
LVL 8

Accepted Solution

by:
wolfgang_93 earned 450 total points
ID: 34936094
Try something like this (note: the temporary table automatically is dropped at the end of
the user session):

CREATE TEMPORARY TABLE idlist (id INTEGER);
INSERT INTO idlist(id) SELECT DISTINCT id FROM images WHERE org_image_id IS NULL and LRU < DATE_SUB(NOW(), INTERVAL 30 DAY);
delete FROM images WHERE org_image_id NOT IN (select id from idlist);
0
 
LVL 26

Expert Comment

by:tigin44
ID: 34936096
also you can try this

DELETE I
FROM images I
      LEFT OUTER JOIN images D ON I.org_image_id = D.id
WHERE D.id IS NULL
  AND D.org_image_id is NULL
 AND I.LRU < DATE_SUB(NOW(), INTERVAL 30 DAY);
0

Featured Post

Webinar: MongoDB® Index Types

Join Percona’s Senior Technical Services Engineer, Adamo Tonete as he presents “MongoDB Index Types, How, When and Where Should They be Used?” on Wednesday, July 12, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

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

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

734 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