C# SQL-一起购买的产品列表

C# SQL-一起购买的产品列表,c#,sql,.net-core,C#,Sql,.net Core,我需要一个SQL查询,以找出哪些产品是与其他产品一起以相同的顺序购买的 我有这张桌子: 医嘱ID 产品ID A111 1. A111 21 A111 12 A111 31 A122 21 A122 43 A122 32 A122 1. A333 12 A333 64 A333 63 A333 21 A333 12 您可以使用自联接: select distinct t1.productid, t2.productid from t t1 join t t2 on t1.ord

我需要一个SQL查询,以找出哪些产品是与其他产品一起以相同的顺序购买的

我有这张桌子:

医嘱ID 产品ID A111 1. A111 21 A111 12 A111 31 A122 21 A122 43 A122 32 A122 1. A333 12 A333 64 A333 63 A333 21 A333 12
您可以使用自联接:

select distinct t1.productid, t2.productid
from t t1 join
     t t2
     on t1.orderid = t2.orderid
where t1.productid <> t2.productid;

此查询花费了很多时间。。。还有其他更快的吗?@DonJuan。select distinct可能是性能问题的原因。我建议了一个替代公式。我做了更改,我想您忘记了join中的on语句:select p1.ProductId,p2.ProductId从ProductTable p1加入p1上的ProductTable p2.ProductId=p2.ProductId,其中存在从OrderHistoryItem t1中选择1加入t1上的OrderHistoryItem t2.OrderHistory\U id=t2.OrderHistory\U id,其中t1.ProductId=p1.ProductId和t2.ProductId=p2.ProductId????没有work@DonJuan . . . 这是为了交叉连接。
select p1.productid, p2.productid
from products p1 cross join
     products p2
where exists (select 1
              from t t1 join
                   t t2
                   on t1.orderid = t2.orderid
              where t1.productid = p1.productid and
                    t2.productid = p2.productid
             );