как-то так думаю он будет выглядеть
select h.name AS host_name, i.name AS item_name, i.key_ as item_key, FROM_UNIXTIME(hs.clock) AS last_time, hs.value, hs.logeventid
from
hosts h
join items i on h.hostid = i.hostid
join history_log hs on i.itemid = hs.itemid
where
DATE_FORMAT(FROM_UNIXTIME(hs.clock/1000), '%Y-%m-%d %H:%i:%s')
BETWEEN STR_TO_DATE('2016-08-01 15:00:00','%Y-%m-%d %H:%i:%s')
AND STR_TO_DATE('2016-08-01 17:00:00','%Y-%m-%d %H:%i:%s')
and i.hostid = '10458' and hs.logeventid = '4624' and hs.logeventid = '4625'
order by last_time, item_name
INTO OUTFILE '/tmp/logon_logoff.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'