You should use JOINS and UNION for such cases
SELECT * FROM (
SELECT msg.*,UNIX_TIMESTAMP(msg.`created_at`) AS unixTime,vs.name FROM `messaging_detail` AS msg JOIN visitor_detail AS vs ON vs.id=msg.send_by WHERE msg.visitor_id = msg.send_by AND msg.visitor_id='18'
UNION
SELECT msg.*,UNIX_TIMESTAMP(msg.`created_at`) AS unixTime,vs.name FROM `messaging_detail` AS msg JOIN login AS vs ON vs.`id`=msg.`send_by` WHERE msg.visitor_id != msg.send_by AND msg.visitor_id='18'
) as a ORDER BY a.created_at ASC
solved Is its possible to output of one query is can input of another sql query in mysql