SELECT
person.idcard AS `เลขประชาชน`,
person.birth AS `วดปเกิด`,
concat(ctitle.titlename,person.fname,' ',person.lname,' ','(',convert(person.pid using utf8),')') AS `ชื่อ - สกุล`,
timestampdiff(year,person.birth,date(NOW())) AS `อายุ`,
person.hnomoi AS `บ้านเลขที่`,
person.mumoi AS `หมู่`,
person.dischargetype
FROM
person
LEFT JOIN ctitle ON person.prename = ctitle.titlecode
WHERE getAgeYearNum(person.birth,CURDATE()) BETWEEN '20' AND '50'
AND typelive like ('1' OR '3')
AND person.pid NOT IN (SELECT person.pid
FROM person INNER JOIN visitepi ON visitepi.pid = person.pid
WHERE (visitepi.vaccinecode like '%DT%') AND visitepi.dateepi BETWEEN '2014-10-01' AND '2015-10-31') and person.dischargetype<>1
ORDER BY mumoi