i have some table like:
Sql Fiddle
I want this table to select only the following values(remove duplicates):
from
| to
46 0
Here is my solution, it seems not so ideal, but it works:
select distinct pm.`from`, pm.`to`
from `tsk_private_message` pm
left join
(select distinct pm.`from`, pm.`to`
from `tsk_private_message` pm
inner join `tsk_private_message` pm2
on (pm.`to` = pm2.`from`) and (pm2.`to` <> pm.`from`)) a
using (`from`, `to`)
where a.`from` is null;
In this query I just search unnecessary rows among these conversations via subquery:
select distinct pm.`from`, pm.`to`
from `tsk_private_message` pm
inner join `tsk_private_message` pm2
on (pm.`to` = pm2.`from`) and (pm2.`to` <> pm.`from`)) a
using (`from`, `to`)
and "subtract" this result from main table.
Here is SQL Fiddle
SELECT DISTINCT a.*
FROM TableName a
INNER JOIN TableName b
ON a.from = b.to
AND a.to = b.from
WHERE a.from < b.from