MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old May 5th, 2008, 09:32 AM
annlis1977 annlis1977 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 9 annlis1977 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 21 m 54 sec
Reputation Power: 0
Delete statement with group by does not work

Hi,

I have 2 tables:

* classified (id, title, status, ...)
* archive (id, classified_id, modification_date, ...)

An archive is a "log of the classifieds modifications".

I would like to delete all the data of a classified where its "status" is "deleted (3)" and where the maximum archive.modification_date is older than 1 year.

The SELECT statement works fine:

Code:
SELECT *
FROM archive a, classified c
WHERE c.status=3 AND a.classified_id = c.id
GROUP BY a.classified_id
HAVING MAX(a.modification_date) < '2007-04-28'


The DELETE statement is not working:

Code:
DELETE a, c
FROM archive a, classified c
WHERE c.status=3 AND a.classified_id = c.id
GROUP BY a.classified_id
HAVING MAX(a.modification_date) < '2007-04-28'


Is there any way to do it without creating a TEMP table?

Thanks for your help.

Best regards,
Fred

Reply With Quote
  #2  
Old May 5th, 2008, 11:24 AM
Veamon's Avatar
Veamon Veamon is offline
Blowing up trees with my mind.
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2005
Location: Neverland Ranch, minus the touching.
Posts: 2,604 Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)Veamon User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 117515 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117515 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117515 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117515 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117515 Folding Title: Super Ultimate Folder - Level 1Folding Points: 117515 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Weeks 3 Days 7 h 41 m 41 sec
Reputation Power: 822
If you have your foreign keys setup properly, and have it set to delete cascade, couldnt you do a

delete from classifieds where status = 3 and modification_date < '2007-04-28'

your archive should have a foreign key that relates it to the classifieds, then it should all go bye bye.
__________________

Raid1 in XP Pro

My open source projects:
------------------------
Blobber - Add images as blobs to SQL Server
------------------------

Reply With Quote
  #3  
Old May 5th, 2008, 12:06 PM
annlis1977 annlis1977 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 9 annlis1977 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 21 m 54 sec
Reputation Power: 0
Quote:
Originally Posted by Veamon
If you have your foreign keys setup properly, and have it set to delete cascade


Unfortunately the tables are not InnoDB => no FOREIGN KEYs.

Reply With Quote
  #4  
Old May 5th, 2008, 12:33 PM
shammat shammat is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 923 shammat User rank is Sergeant Major (2000 - 5000 Reputation Level)shammat User rank is Sergeant Major (2000 - 5000 Reputation Level)shammat User rank is Sergeant Major (2000 - 5000 Reputation Level)shammat User rank is Sergeant Major (2000 - 5000 Reputation Level)shammat User rank is Sergeant Major (2000 - 5000 Reputation Level)shammat User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 2 h 18 m 59 sec
Reputation Power: 54
Quote:
Originally Posted by annlis1977
I have 2 tables:

* classified (id, title, status, ...)
* archive (id, classified_id, modification_date, ...)

An archive is a "log of the classifieds modifications".

I would like to delete all the data of a classified where its "status" is "deleted (3)" and where the maximum archive.modification_date is older than 1 year.

I don't think it possible to DELETE from two tables in a single statement. So you have to split this up in two statements.
Something like this (untested!)
Code:
DELETE FROM archive
WHERE modification_date < '2007-04-28'
AND classified_id IN (SELECT id 
                      FROM classified 
                      WHERE status = 3);

DELETE FROM classified
WHERE status = 3;

Reply With Quote
  #5  
Old May 5th, 2008, 02:46 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,727 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 19 h 22 m 42 sec
Reputation Power: 848
Quote:
Originally Posted by shammat
I don't think it possible to DELETE from two tables in a single statement.
but it is
__________________
r937.com | rudy.ca

Reply With Quote
  #6  
Old May 5th, 2008, 02:49 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,727 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 19 h 22 m 42 sec
Reputation Power: 848
Quote:
Originally Posted by annlis1977
I would like to delete all the data of a classified where ...
this sounds like you want to delete from only one table

your DELETE statement is attempting to delete from two



try something like
Code:
DELETE c -- only ;o)
FROM archive a, classified c
WHERE c.status=3 AND a.classified_id = c.id
GROUP BY a.classified_id
HAVING MAX(a.modification_date) < '2007-04-28'

caution: i'm not saying this is gonna work, i'm saying that if you only want to delete classified rows then that's the only table you should mention in the DELETE part, although of course you can still have a join in the FROM part


Reply With Quote
  #7  
Old May 5th, 2008, 03:29 PM
shammat shammat is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 923 shammat User rank is Sergeant Major (2000 - 5000 Reputation Level)shammat User rank is Sergeant Major (2000 - 5000 Reputation Level)shammat User rank is Sergeant Major (2000 - 5000 Reputation Level)shammat User rank is Sergeant Major (2000 - 5000 Reputation Level)shammat User rank is Sergeant Major (2000 - 5000 Reputation Level)shammat User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 2 h 18 m 59 sec
Reputation Power: 54
Quote:
Originally Posted by r937
but it is
Deleting rows from two tables in a single statement?
This must be a MySQL extension then, can you show me an example?

Reply With Quote
  #8  
Old May 5th, 2008, 03:42 PM
annlis1977 annlis1977 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 9 annlis1977 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 21 m 54 sec
Reputation Power: 0
Quote:
Originally Posted by shammat
Deleting rows from two tables in a single statement?
This must be a MySQL extension then, can you show me an example?


Right, I wanted to deleted the rows from the 2 tables which is possible in MySQL.

http://dev.mysql.com/doc/refman/5.0/en/delete.html

"Currently, you cannot delete from a table and select from the same table in a subquery."

Maybe the "GROUP BY.... HAVING" is the same as a subquery...

Reply With Quote
  #9  
Old May 5th, 2008, 04:36 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,727 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 19 h 22 m 42 sec
Reputation Power: 848
Quote:
Originally Posted by shammat
This must be a MySQL extension then

Reply With Quote
  #10  
Old May 6th, 2008, 01:19 PM
crazytrain81 crazytrain81 is offline
Always Learning
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2002
Location: Port Neches, TX, USA
Posts: 1,173 crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level)crazytrain81 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 7 h 45 m 43 sec
Reputation Power: 10
Send a message via AIM to crazytrain81 Send a message via MSN to crazytrain81
Makes sense that GROUP BY is equivalent contextually to using a subquery here - when you GROUP BY, you go from a set of individual rows to a single aggregated row - so the DELETE no longer has a set containing each row to be deleted, instead it has just a row of aggregate data.

If you're using 5 - using a procedure w/ a cursor. Select, then go through the cursor and delete rows.
__________________
David Fells
If my post helped you, please click the above my post and leave a comment. Thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Delete statement with group by does not work


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |