Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 比较顺序无关紧要的配对_Mysql_Sql_Relationship - Fatal编程技术网

Mysql 比较顺序无关紧要的配对

Mysql 比较顺序无关紧要的配对,mysql,sql,relationship,Mysql,Sql,Relationship,我想压缩表中的数据,这样就不会有对序列方面的冗余,例如a,b与b相同,a作为对 具体来说,我想从表中得到:冗余_关系 +------+------+------+ | p1 | p2 | score| +------+------+------+ | a | b | 0.8 | | a | c | 0.67 | | b | a | 0.8 | | c | a | 0.67 | | a | d | 0.89 | | a

我想压缩表中的数据,这样就不会有对序列方面的冗余,例如a,b与b相同,a作为对

具体来说,我想从表中得到:冗余_关系

+------+------+------+
| p1   | p2   | score|
+------+------+------+
| a    | b    |  0.8 |
| a    | c    | 0.67 |
| b    | a    |  0.8 |
| c    | a    | 0.67 |
| a    | d    | 0.89 |
| a    | e    | 0.47 |
| d    | a    | 0.89 |
| e    | a    | 0.47 |
+------+------+------+


在这里,我只想选择第一个关系并放弃反向关系,例如,如果A和B是分数为0.8的朋友,我想为他们的关系保留一行[A,B,0.8],而不是两行,即[A,B,0.8]和[B,A,0.8],我已经有一个表存在这些关系,我想删除后面的关系。

如果您知道您有所有对,那么只需执行以下操作:

select rr.*
from redundant_relations rr
where rr.p1 < rr.p2;
select rr.*
from redundant_relations rr
where rr.p1 < rr.p2;
select rr.*
from redundant_relations rr
where rr.p1 < rr.p2
union all
select rr.*
from redundant_relations rr
where rr.p1 > rr.p2 and
      not exists (select 1
                  from redundant_relations rr2
                  where rr2.p1 = rr.p2 and rr2.p2 = rr.p1 and rr2.score = rr.score
                 );