简体   繁体   English

如何在SQL中从对中查找所有三元组?

[英]How to find all triples from pairs in SQL?

Assume that I use SQLite query syntax. 假设我使用SQLite查询语法。

I have a table of distances between points: 我有一张表之间的距离表:

CREATE TABLE d (
    p1 INT,
    p2 INT,
    distance REAL
);

If I know distance between p1 and p2 then I known distance between p2 and p1 - both rows are in table. 如果我知道p1p2之间的距离,那么我知道p2p1之间的距离-这两行都在表中。

I want to create a view ( select query) for all unique triples of points, so that I know the distances between all three of them pairwise. 我想为所有唯一的三元组创建一个视图( select查询),以便我知道所有这三个对之间的距离。

I tried the following: 我尝试了以下方法:

CREATE VIEW triple as
    select d1.p1 as p1, d2.p1 as p2, d3.p1 as p3
    from d as d1, d as d2, d as d3
    where d1.p1=d3.p2 and d1.p2=d2.p1 and d2.p2=d3.p1;

But I don't know how to get rid of transpositions like: 但是我不知道如何摆脱像这样的换位:

1 | 2 | 3
2 | 3 | 1
3 | 1 | 2

What is a fast and correct select query for my view? 什么是对我的视图的快速正确的select查询?

If you have all pairs, you can use pair-wise comparisons. 如果有所有配对,则可以使用成对比较。 Here is one method: 这是一种方法:

CREATE VIEW triple as
    select d1.p1 as p1, d2.p1 as p2, d3.p1 as p3
    from d d1 join
         d d2
         on d1.p2 = d2.p1 join
         d as d3
         on d1.p1 = d3.p2 and d2.p2 = d3.p1
    where d1.p1 < d1.p2 and d3.p1 < d3.p2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM