DELETE
vn.*
FROM
visitnutrition vn
inner JOIN
(SELECT 
DISTINCT  
visit.pcucode, 
visit.pid, 
visit.visitno AS seq,  
IF(visit.visitdate IS NULL OR TRIM(visit.visitdate)='' OR visit.visitdate LIKE '0000-00-00%','',DATE_FORMAT(visit.visitdate,'%Y%m%d')) AS date_serv,  
person.sex,  
getAgeMonth(person.birth,CURDATE()) AS agemonth,  
IF(visitnutrition.weight IS NOT NULL AND TRIM(visitnutrition.weight)<>'', REPLACE(FORMAT(visitnutrition.weight,2),',',''), FORMAT(0,2)) AS weightkg,  
IF(visitnutrition.tall IS NOT NULL AND TRIM(visitnutrition.tall)<>'', REPLACE(FORMAT(visitnutrition.tall,2),',',''), FORMAT(0,2)) AS heightcm,  visitnutrition.nlevel AS nlevel,  
IF( visitnutrition.dateupdate IS NULL OR TRIM(visitnutrition.dateupdate)='' OR visitnutrition.dateupdate LIKE '0000-00-00%',DATE_FORMAT(visit.visitdate,'%Y%m%d%H%i%s'),DATE_FORMAT(visitnutrition.dateupdate,'%Y%m%d%H%i%s') ) AS d_update  ,
idcard as cid FROM visitnutrition  INNER JOIN visit ON (visitnutrition.pcucode=visit.pcucode AND visitnutrition.visitno=visit.visitno)  INNER JOIN person ON (visit.pcucodeperson=person.pcucodeperson AND visit.pid=person.pid) 
WHERE person.nation IN('99','099') 
AND (visit.flagservice='01' OR visit.flagservice='02' OR visit.flagservice='03' OR visit.flagservice IS NULL OR TRIM(visit.flagservice)='')  AND TRIM(visitnutrition.pcucode)<>'' AND ((visitnutrition.weight IS NOT NULL AND visitnutrition.weight>0) OR (visitnutrition.tall IS NOT NULL AND visitnutrition.tall>0))
AND (visit.visitdate >= '2013-11-01') AND (visit.visitdate BETWEEN '2013-11-01' AND '2013-11-30')
having agemonth > 226) as tmp
on vn.pcucode = tmp.pcucode and vn.visitno = tmp.seq