Solved

Need help with a MySQL Query

Posted on 2011-02-19
3
432 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

738 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