SELECT person.pid
,CONCAT_WS('-',substring(idcard,1,1),substring(idcard,2,4),substring(idcard,6,5),substring(idcard,11,2),substring(idcard,13,1))AS 'เลขบัตรประชาชน'
,CONCAT(ctitle.titlename,' ',person.fname,' ',person.lname) AS 'ชื่อ-สกุล'
,CONCAT(DATE_FORMAT(person.birth,'%d-%m-'),DATE_FORMAT(person.birth,'%Y')+543) AS 'วันเดือนปีเกิด'
,YEAR( FROM_DAYS( DATEDIFF( NOW( ) ,person.birth ) ) ) AS 'อายุ'
,person.hnomoi as 'บ้านเลขที่'
,SUBSTRING(house.villcode,7,2) AS 'หมู่ที่'
FROM person
LEFT JOIN ctitle ON person.prename = ctitle.titlecode
LEFT JOIN persondeath ON person.pcucodeperson = persondeath.pcucodeperson AND person.pid = persondeath.pid
LEFT JOIN house ON house.pcucode = person.pcucodeperson AND house.hcode = person.hcode
LEFT JOIN village ON village.pcucode = house.pcucode AND village.villcode = house.villcode
WHERE persondeath.pid IS NULL
and person.sex = '2'
AND getAgeYearNum (birth,CURDATE()) BETWEEN 30 and 70
AND person.typelive in ('0','1','3')
AND right((house.villcode),2) != 00
and person.pid  in (select visitlabcancer.pid from visitlabcancer where typecancer = '1'
and datecheck between '20131001' and '20140930')
ORDER BY villno,hnomoi*1;
#create by rambo