SQLite查询联接逻辑

时间:2019-02-13 23:42:04

标签: sql

我能够在excel中对其进行计算,但是发现在SQLite中提出正确的逻辑具有挑战性。

到目前为止,我已经尝试过:

   SELECT ProductName, productID, OrderDate
     FROM Product
LEFT JOIN OrderDetails 
       ON Product.ProductId = OrderDetails.ProductId
LEFT JOIN Orders 
       ON Orders.OrderId = OrderDetails.OrderId
    WHERE OrderDate 
  BETWEEN '1996/10/01' AND '1997/01/01' and 
          ProductID IS NULL
 GROUP BY ProductName

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

在我看来,您可以简单地使用NOT EXISTS条件来过滤在给定日期间隔内没有Orders的客户,例如:

SELECT c.*
FROM Customers c
WHERE NOT EXISTS (
    SELECT 1
    FROM Orders o
    WHERE 
        o.CustomerID = c.CustomerID 
        AND o.OrderDate BETWEEN '1996/10/01' AND '1997/01/01'
)

这也可以结合使用LEFT JOINIS NULL条件来完成:

SELECT c.*
FROM Customers c
LEFT JOIN Orders o
    ON o.CustomerID = c.CustomerID 
    AND o.OrderDate BETWEEN '1996/10/01' AND '1997/01/01'
WHERE o.OrderID IS NULL

在W3School提琴中,两个查询均返回57条记录。

答案 1 :(得分:0)

您可以使用查询的变体形式获取过去3个月内订购的所有不同产品

SELECT DISTINCT Products.ProductID
FROM Products
INNER JOIN OrderDetails ON Products.ProductId = OrderDetails.ProductId
INNER JOIN Orders ON Orders.OrderId = OrderDetails.OrderId
WHERE Orders.OrderDate BETWEEN '1996/10/01' AND '1997/01/01'

现在,您可以使用上述查询来获取过去3个月内订购的所有产品:

SELECT p.ProductID, p.ProductName
FROM Products p LEFT JOIN (
    SELECT DISTINCT Products.ProductID 
    FROM Products
    INNER JOIN OrderDetails ON Products.ProductId = OrderDetails.ProductId
    INNER JOIN Orders ON Orders.OrderId = OrderDetails.OrderId
    WHERE Orders.OrderDate BETWEEN '1996/10/01' AND '1997/01/01'
) t 
ON t.ProductID = p.ProductID
WHERE t.ProductID IS NULL

或:

SELECT ProductID, ProductName
FROM Products 
WHERE ProductID NOT IN (
    SELECT DISTINCT Products.ProductID
    FROM Products
    INNER JOIN OrderDetails ON Products.ProductId = OrderDetails.ProductId
    INNER JOIN Orders ON Orders.OrderId = OrderDetails.OrderId
    WHERE Orders.OrderDate BETWEEN '1996/10/01' AND '1997/01/01'
)

两个查询均返回19种产品:

ProductID   ProductName
4           Chef Anton's Cajun Seasoning
5           Chef Anton's Gumbo Mix
6           Grandma's Boysenberry Spread
7           Uncle Bob's Organic Dried Pears
8           Northwoods Cranberry Sauce
10          Ikura
15          Genen Shouyu
18          Carnarvon Tigers
20          Sir Rodney's Marmalade
24          Guaraná Fantástica
25          NuNuCa Nuß-Nougat-Creme
32          Mascarpone Fabioli
39          Chartreuse verte
43          Ipoh Coffee
45          Røgede sild
51          Manjimup Dried Apples
52          Filo Mix
58          Escargots de Bourgogne
67          Laughing Lumberjack Lager 

使用EXCEPT也可以得到相同的结果:

SELECT p.ProductID, p.ProductName 
FROM Products p INNER JOIN (
    SELECT ProductID
    FROM Products 
    EXCEPT 
    SELECT DISTINCT Products.ProductID
    FROM Products
    INNER JOIN OrderDetails ON Products.ProductId = OrderDetails.ProductId
    INNER JOIN Orders ON Orders.OrderId = OrderDetails.OrderId
    WHERE Orders.OrderDate BETWEEN '1996/10/01' AND '1997/01/01'
) t ON t.ProductID = p.ProductID