我希望此表仅选择以下值(删除重复项):
from
| to
46 0
46 18
46 50
46 39
46 11
46 12
46 56
我使用DISTINCT关键字,但这会返回以下值:
46 0
46 18
46 50
46 39
46 11
11 46
46 12
46 56
请帮帮我
答案 0 :(得分:3)
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
答案 1 :(得分:1)
这是我的解决方案,似乎不太理想,但它确实有效:
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;
在这个查询中,我只是通过子查询在这些对话中搜索不必要的行:
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`)
并从主表中“减去”此结果。