SELECT person.pid,
CONCAT(ctitle.titlename,' ',person.fname,' ',person.lname) AS 'ชื่อ-นามสกุล',
person.idcard AS 'เลขบัตรประชาชน',
concat(substr(person.birth,9,2),'/',substr(person.birth,6,2),'/',substr(person.birth,1,4)+543) as 'วันเดือนปีเกิด',
getAgeYearNum(person.birth,CURDATE()) AS `อายุ`,concat(person.hnomoi,' ','ม.',person.mumoi)as 'ที่อยู่่',
visitepi.vaccinecode,
visitepi.dateepi AS 'วันที่ฉีดวัคซีน'
FROM person
LEFT JOIN ctitle ON person.prename = ctitle.titlecode
LEFT JOIN visitepi ON person.pcucodeperson=visitepi.pcucodeperson
#LEFT JOIN visitanc ON 
AND person.pid=visitepi.pid
WHERE visitepi.vaccinecode='DTC'
AND GetAgeYearNum(person.birth,'2013-10-01') between '20' and '50'
AND dateepi BETWEEN '2014-10-01' AND '2014-12-31'
AND nation='99'
GROUP BY visitepi.pid;

