If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Help with delete statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-08-10, 16:11
tjones1105 tjones1105 is offline
Registered User
 
Join Date: Dec 2007
Posts: 21
Help with delete statement

Hello,
I have a table full of client inventory data and there is a lot of duplicate data from older inserts I would like to remove. I have not done very many complex remove statements so I could use some help.

Table
my_inv_data
Cols
rid (autoincr, id)
client_id
date
name
path


What I'm looking to do is just keep records which match the MAX(`date`) for each client_id.

Thanks,
tom
Reply With Quote
  #2 (permalink)  
Old 11-09-10, 09:26
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Code:
delete from table a
where a.date < (select max(b.date) from table b
                where a.client_id = b.client_id)
Dave
Reply With Quote
  #3 (permalink)  
Old 11-10-10, 20:07
tjones1105 tjones1105 is offline
Registered User
 
Join Date: Dec 2007
Posts: 21
thanks,
but I cant seem to get it to work...

Quote:
error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where a.date < ( select MAX(b.date) from my_inv_data b where a. client_id = b.' at line 2

tom
Reply With Quote
  #4 (permalink)  
Old 11-11-10, 07:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
i think the alias in the outer query is wrong

try like this --
Code:
DELETE 
  FROM my_inv_data 
 WHERE date < ( SELECT MAX(b.date) 
                  FROM my_inv_data AS foo
                 WHERE foo.client_id = daTable.client_id )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 11-15-10, 16:22
tjones1105 tjones1105 is offline
Registered User
 
Join Date: Dec 2007
Posts: 21
Thanks,
but now I get this error...

Quote:
Error Code: 1093
You can't specify target table 'my_inv_data' for update in FROM clause
tom
Reply With Quote
  #6 (permalink)  
Old 11-15-10, 19:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
seems like you can't select and delete from the same table

try creating a temproray table with the MAXes in it, then DELETE WHERE NOT EXISTS...
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On