如何从 sql 中的名称对中选择不同的值?

how to choose distinct values from the pairs of names in sql?

table f 具有这些值:

name1           name2           count
Harrison Ford   Mullah Omar     3
Harrison Ford   Michael Caine   6
Michael Caine   Mullah Omar     2
Michael Caine   Harrison Ford   6
Mullah Omar     Michael Caine   2
Mullah Omar     Harrison Ford   3

如何只选择不同的对?

select 
distinct name1, 
distinct name2, count from f group by name1

鉴于每对都有一个副本,您可以使用:

select f.*
from f
where name1 < name2;

如果您在所有情况下都没有反转,您可以使用:

select f.*
from f
where f.name1 < f.name2 or
      not exists (select 1 
                  from f f2
                  where f2.name1 = f.name2 and f2.name2 = f.name1
                 );