SELECT
	p.pid,
	p.idcard,
	c.titlename,
	p.fname,
	p.lname,
	p.sex,
	DATE_FORMAT(p.birth, '%d/%m/%Y') AS birth,
	a.mu,
	a.hno,
	CONCAT(
		a.provcode,
		a.distcode,
		a.subdistcode
	) AS aid,
	p.hosmain,
	p.hossub,
	p.nation,
	'2' AS his
FROM
	person p
LEFT OUTER JOIN personaddresscontact a ON p.pid = a.pid
LEFT OUTER JOIN ctitle c ON p.prename = c.titlecode
LEFT JOIN persondeath ON p.pid = persondeath.pid
WHERE
	a.mu <> '0'
AND (
	p.idcard IS NOT NULL
	OR p.idcard <> ''
)
AND persondeath.pid IS NULL
AND getagemonth (p.birth, '2014-10-01') BETWEEN 6
AND 48
AND nation = 99
AND p.typelive IN ('1', '3')