On Fri, Jan 21, 2011 at 10:20 AM, Alvaro Carrasco <[email protected]> wrote:
> On Fri, Jan 21, 2011 at 9:59 AM, Wade Preston Shearer
> <[email protected]> wrote:
>> I have an MySQL table with a foreign key and a created date. I'd like to 
>> delete all rows that match the foreign key except he most recent one. Is 
>> that possible in a single query?
>>
>
> Something like this should do it:
>
> delete table1 from table1
> inner join table2 on table1.table2_id = table2.id
> where table2.created_date > '2011-01-01'
>
> Alvaro
>

Of course if you want everything except the most recent, use "<"instead of ">".

The query is untested. Make sure to run it on a test database before
you run it on important data.

Alvaro

_______________________________________________

UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net

Reply via email to