How to remove two duplicate column

后端 未结 2 1901
南旧
南旧 2021-01-16 11:15

i have some table like:
Sql Fiddle

I want this table to select only the following values(remove duplicates):
from | to
46 0

相关标签:
2条回答
  • 2021-01-16 11:49

    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

    0 讨论(0)
  • 2021-01-16 11:52
    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
    
    • SQLFiddle Demo
    0 讨论(0)
提交回复
热议问题