Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.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
SQL匹配来自2个表的数据_Sql_Sqlite - Fatal编程技术网

SQL匹配来自2个表的数据

SQL匹配来自2个表的数据,sql,sqlite,Sql,Sqlite,我有两张桌子 'Order details' OrderID | Quantity | UnitPrice | ProductID 1002 | 19 | 17 | 824 1002 | 5 | 15 | 285 1003 | 7 | 17 | 824 1003 | 7 | 15 | 285 1003 | 7 |

我有两张桌子

'Order details'
OrderID | Quantity | UnitPrice | ProductID
  1002  |    19    |    17     |    824
  1002  |    5     |    15     |    285
  1003  |    7     |    17     |    824
  1003  |    7     |    15     |    285
  1003  |    7     |    11     |    205
  1004  |    12    |    11     |    205


'Orders'
OrderID | CustomerID
  1002  |   224
  1003  |   348
  1004  |   224
我需要找到与另一个CustomerID具有相同orders ProductID的CustomerID,例如ID号224。所有的订单都必须接受,我是说所有的订单ID。
因此,输出为348,因为此id在其订单中具有完全相同的ProductID。

在下面的草图中,我列出了所有订单,这些订单具有完全相同的客户id 224所下订单的产品

select distinct o2.OrderId
from    
    Orders o1, Orders o2
where 
    o1.CustomerId = 224
    and o1.OrderId <> o2.OrderId
    and not exists (
        select 1 
        from 
            "Orders details" od1 
            full outer join "Order details" od2 on 
                od1.OrderId = o1.OrderId 
                and od2.OrderId = o2.OrderId 
                and od1.ProductId = od2.ProductId
        where
            od1.OrderId is null or od2.OrderId is null
    )
剩下的应该很简单

如果DBMS不支持完全外部联接,可以将其拆分为两个左联接,如下所示:

select distinct o2.OrderId
from    
    Orders o1, Orders o2
where 
    o1.CustomerId = 224
    and o1.OrderId <> o2.OrderId
    and not exists (
        select 1 
        from 
            "Orders details" od1 
            left join "Order details" od2 on 
                od1.OrderId = o1.OrderId 
                and od2.OrderId = o2.OrderId 
                and od1.ProductId = od2.ProductId
        where
            od2.OrderId is null
    )
    and not exists (
        select 1 
        from 
            "Orders details" od1 
            left join "Order details" od2 on 
                od1.OrderId = o2.OrderId 
                and od2.OrderId = o1.OrderId 
                and od1.ProductId = od2.ProductId
        where
            od2.OrderId is null
    )

如果您希望客户使用相同的产品:

with od1 as (
      select distinct o.customerid, od.productid
      from orderdetails od join
           orders o
           on o.orderid = od.orderid
     ),
     od as (
      select od1.*,
             (select count(*) from od1 od2 where od2.customerid = od1.customerid) as numproducts
      from od1
     )
select od.customerid
from od od join
     od od2
     on od.productid = od2.productid and od.numproducts = od2.numproducts and
        od.customerid = 224
group by od.customerid
having count(*) = od.numproducts;
CTE的目的只是为每个客户、产品和产品计数获取一行。外部查询统计两个客户之间的匹配数。匹配的数量需要与产品的数量匹配

这将返回完全匹配的结果。第二个客户必须拥有完全相同的产品,不多不少


注意:这将返回使用where子句轻松过滤掉的原始客户。

您能澄清一下吗?客户224关联了两个订单,这是您想要查找的吗?你能从你发布的示例数据中指定你期望的输出吗?@MassimilianoCarosi的可能副本更新了这个问题,希望它更清楚:如果客户有更多的产品怎么办?@GordonLinoff他们有完全相同的产品是很重要的。如果您指的是数量列,那就不重要了。非常感谢您,不幸的是,我有一个错误“循环引用:od”,稍后我将分成两个左连接。@MassimilianoCarosi好的,我将wait@OleksiiKryzh . . . 我解决了这个问题;我只是在CTE中使用了错误的别名。@GordonLinoff很抱歉,但没有这样的列:od.customerid,“订单详细信息”表中没有列customerid非常感谢,但我有一个错误“当前不支持正确和完整的外部联接”。也许还有其他方法可以解决这个问题?添加了一个没有完全外部连接的版本,希望能有所帮助。但是我应该把我想与其他人比较的customerid放在哪里?这应该会给你所有匹配的customerid对
with od1 as (
      select distinct o.customerid, od.productid
      from orderdetails od join
           orders o
           on o.orderid = od.orderid
     ),
     od as (
      select od1.*,
             (select count(*) from od1 od2 where od2.customerid = od1.customerid) as numproducts
      from od1
     )
select od.customerid
from od od join
     od od2
     on od.productid = od2.productid and od.numproducts = od2.numproducts and
        od.customerid = 224
group by od.customerid
having count(*) = od.numproducts;