// удаляем некорректные аттачи
stmt = con.createStatement();
stmt.executeUpdate("DELETE mail_attachments FROM mail_attachments LEFT JOIN items ON mail_attachments.itemId = items.object_id WHERE items.object_id IS NULL");
DatabaseUtils.closeStatement(stmt);
// чистим письма с потерянными аттачами
stmt = con.createStatement();
stmt.executeUpdate("UPDATE mail LEFT JOIN mail_attachments ON mail.messageId = mail_attachments.messageId SET price=0,attachments=0 WHERE mail_attachments.messageId IS NULL");
DatabaseUtils.closeStatement(stmt);
// чистим от мусора в mail_attachments, возвращая вещи владельцам
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT itemId FROM mail_attachments LEFT JOIN mail ON mail.messageId = mail_attachments.messageId WHERE mail.messageId IS NULL");