SELECT
person.pid AS pid,
concat(ctitle.titlename,person.fname,' ',person.lname)as pname,
date_format(person.birth,"%Y-%m-%d") AS pbirth,
GetAgeYearNum(person.birth,CURRENT_DATE)as age,
person.idcard AS pcid,
person.sex AS sex,
person.hnomoi AS hno,
person.mumoi AS mu,
village.villno AS village_villno,
village.villname AS village_villname
FROM
person INNER JOIN ctitle ON person.prename = ctitle.titlecode
INNER JOIN house ON person.pcucodeperson = house.pcucode
AND person.hcode = house.hcode
INNER JOIN village ON house.pcucode = village.pcucode
AND village.villcode = house.villcode
WHERE
person.typelive IN ('1','3')
and person.pid not in (SELECT persondeath.pid FROM persondeath)
and GetAgeYearNum(person.birth,CURRENT_DATE) >= '6'
and GetAgeYearNum(person.birth,CURRENT_DATE) <= '24'
and SUBSTRING(house.villcode,7,2)<>'00'
order BY village.villno,person.hnomoi*1;