|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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 ------------------------ |
|
#3
|
|||
|
|||
|
Quote:
Unfortunately the tables are not InnoDB => no FOREIGN KEYs. |
|
#4
|
|||
|
|||
|
Quote:
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;
|
|
#5
|
||||
|
||||
|
Quote:
![]() |
|
#6
|
||||
|
||||
|
Quote:
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 ![]() |
|
#7
|
|||
|
|||
|
Quote:
This must be a MySQL extension then, can you show me an example? |
|
#8
|
|||
|
|||
|
Quote:
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... |
|
#9
|
||||
|
||||
|
Quote:
![]() |
|
#10
|
|||
|
|||
|
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
|
![]() |
| Viewing: Dev Shed Forums > Databases > MySQL Help > Delete statement with group by does not work |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|