select i1.*,i2.*,concat(p.pname,p.fname," ",p.lname) as fullname,
concat(p.addrpart," หมู่ ",p.moopart," ต.",t3.name," อ.",t2.name," จ.",t1.name) as fulladdressname,
p.*,id.*,pty.name as pttype_name,w.name as ward_name ,d1.*,t1.name as chwname,t2.name as ampname,
t3.name as tmbname
from ipt i1,iptadm id,an_stat i2,patient p,pttype pty,ward w
left outer join doctor d1 on d1.code=i1.admdoctor
left outer join thaiaddress t1 on t1.chwpart=p.chwpart and
t1.amppart="00" and t1.tmbpart="00"
left outer join thaiaddress t2 on t2.chwpart=p.chwpart and
t2.amppart=p.amppart and t2.tmbpart="00"
left outer join thaiaddress t3 on t3.chwpart=p.chwpart and
t3.amppart=p.amppart and t3.tmbpart=p.tmbpart
where i1.an=i2.an and i1.hn=p.hn and id.an=i1.an and pty.pttype=i2.pttype and w.ward=i1.ward and i1.an="490001550"