Solved

Need help with a MySQL Query

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…

776 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