( SELECT e.eml_date as eldia, e.eml_subject as Note, ifnull(a.att_file_name,'') as FILES, e.eml_from as USERS, e.eml_auth_level as Isemail, e.eml_code as Note_Code
FROM
email as e
JOIN
negotiation as n
ON
e.eml_ngt_code=n.ngt_code
and e.eml_ngt_code='1'
and n.ngt_code='1'
AND e.eml_date BETWEEN '1999-10-01' AND '2100-12-01'
JOIN
contact as c
ON
e.eml_cnt_code=c.cnt_code
and n.ngt_code=e.eml_ngt_code
LEFT JOIN
attachments as a ON a.att_eml_code=e.eml_code
)
UNION
(
SELECT h.hst_date as eldia, h.hst_note as Note, ifnull(d.dcm_file_name,'') as FILES, h.hst_crt_by as USERS, h.hst_auth_level as Isemail, h.hst_code as Note_Code
FROM history as h
JOIN
negotiation as n
ON h.hst_ngt_code=n.ngt_code
and n.ngt_code='1'
AND h.hst_date BETWEEN '1999-10-01' AND '2100-12-01'
LEFT JOIN
documents as d
ON d.dcm_hst_code=h.hst_code
)
ORDER BY eldia DESC;