SELECT 'cid','pname','fname','lname','birth','sex','address','moo','aid','screen_date','screen_status','screen_right','screen_left'
UNION
select * from
(
SELECT p.idcard cid,ctitle.titlename pname,p.fname,p.lname,p.birth,p.sex,house.hno address,village.villno moo,
left(house.villcode,6)aid,v.visitdate screen_date,'Y' screen_status,'Y' screen_right,'Y' screen_left
FROM visit v
INNER JOIN visitdiag vd ON vd.pcucode = v.pcucode AND vd.visitno = v.visitno
INNER JOIN person p ON v.pcucodeperson = p.pcucodeperson AND v.pid = p.pid
left join ctitle on p.prename = ctitle.titlecode
inner join house on p.hcode = house.hcode and p.pcucodeperson = house.pcucode
inner join village on house.villcode = village.villcode and house.pcucode = village.pcucode
where vd.diagcode IN ('H54.5','H54.1','H54.4','H54.0','Z01.0')
and v.visitdate BETWEEN '2015-10-01' and '2016-09-30'
GROUP BY p.pid
)a
INTO OUTFILE 'C:\\JHCIS_export\\screen_va.txt'
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY ''
LINES TERMINATED BY '\r\n';