Нужно сделать вот такой запрос:
CREATE TEMPORARY TABLE user_id( id INT(11), senddate DATETIME ) TYPE = MYISAM; INSERT INTO user_id (id, senddate) SELECT to_id , senddate FROM cms_user_msg WHERE from_id = 1; INSERT INTO user_id (id, senddate) SELECT from_id, senddate FROM cms_user_msg WHERE to_id = 1; SELECT DISTINCT d.id , u.login , u.nickname , u.is_deleted , p.imageurl FROM user_id d LEFT JOIN cms_users u ON u.id = d.id LEFT JOIN cms_user_profiles p ON p.user_id = d.id