Solved

Need help with a MySQL Query

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Creating Functions in phpMyAdmin 8 26
Database needed for HR files 4 58
MySql Recovery 2 29
Uploading a CSV Data Import via PHP & MySql 3 35
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

820 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