SELECT CASE WHEN useridfrom > useridto THEN '' || useridfrom || '-' || useridto ELSE '' || useridto || '-' || useridfrom END AS convo_signature, m.smallmessage, m.id, m.useridto, m.useridfrom, m.timeread FROM (SELECT id, useridfrom, useridto, smallmessage, 0 as timeread FROM mdl_message WHERE (id = 4 AND timecreated = 1486606496) UNION ALL SELECT id, useridfrom, useridto, smallmessage, timeread FROM mdl_message_read WHERE (id = 4 AND timecreated = 1486606496)) m Empty set (1.89 sec) +----+--------------+------------+------+---------------+------+---------+------+------+-----------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------+------------+------+---------------+------+---------+------+------+-----------------------------------------------------+ | 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 2 | NULL | | 2 | DERIVED | NULL | NULL | NULL | NULL | NULL | NULL | NULL | no matching row in const table | | 3 | UNION | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE noticed after reading const tables | | NULL | UNION RESULT | | ALL | NULL | NULL | NULL | NULL | NULL | Using temporary | +----+--------------+------------+------+---------------+------+---------+------+------+-----------------------------------------------------+ 4 rows in set (0.16 sec)