SELECT person.pid
,CONCAT(ctitle.titlename,person.fname,' ',person.lname) AS ' ชื่อ-สกุล'
,CONCAT_WS('-',substring(idcard,1,1),substring(idcard,2,4),substring(idcard,6,5),substring(idcard,11,2),substring(idcard,13,1))AS idcard
,CONCAT(DATE_FORMAT(person.birth,'%d-%m-'),DATE_FORMAT(person.birth,'%Y')+543)as วันเกิด
,getAgeYearNum(person.birth,('2014-12-31')) AS age
,YEAR(person.birth)+543 AS yearr
,house.hno
,right((house.villcode),2) AS moo
FROM person
INNER JOIN house ON person.hcode = house.hcode
INNER JOIN ctitle ON person.prename = ctitle.titlecode
LEFT JOIN village on house.villcode = village.villcode
LEFT JOIN persondeath ON person.pid = persondeath.pid
WHERE (YEAR(person.birth)+543) BETWEEN 2487 AND 2527
AND person.sex = 2
AND persondeath.pid IS NULL
AND person.typelive in ('0','1','3')
AND right((house.villcode),2) != 00
ORDER BY moo,hno*1;