SELECT
person.pid,
person.idcard,
ctitle.titlename,
person.fname,
person.lname,
CONCAT(DATE_FORMAT(person.birth,'%d-%m-'),DATE_FORMAT(person.birth,'%Y')+543)as วันเกิด,
getAgeYearNum(person.birth,('2014-07-01')) AS age,
house.hno, right((house.villcode),2) AS moo,
personchronic.chroniccode,
personchronic.cup
FROM person
INNER JOIN house ON person.hcode = house.hcode
INNER JOIN ctitle ON person.prename = ctitle.titlecode
INNER JOIN personchronic on person.pid=personchronic.pid
LEFT JOIN persondeath ON person.pid = persondeath.pid
where personchronic.cup<>person.pcucodeperson
AND persondeath.pid is null
GROUP BY pid
ORDER BY moo;
 