0

我有几张桌子。我只想写一个查询,它给了我没有下订单的日期。我正在使用左连接来获得 null 的结果,但问题是:使用它后我无法获得日期。

请帮助我,我的代码是这样的:

SELECT DAY(o.OrderDate) AS 'HavntRecivedOrder'
  FROM Products p
 INNER JOIN Suppliers sup   ON p.SupplierID = sup.SupplierID
 INNER JOIN OrderDetails od ON p.ProductID = od.ProductID
  LEFT JOIN Orders o        ON (od.OrderID = o.OrderID
                                AND o.OrderDate BETWEEN '1/1/1997' AND '1/31/1997')
 WHERE sup.CompanyName='Tokyo Traders'
   AND o.OrderDate IS  NULL
4

1 回答 1

0

请发布所需的表结构以更好地理解问题。

尽管如此,从您的问题描述中,我了解到您希望有几天没有发布订单,对吗?那我不明白你为什么要写这么复杂的查询。也许就足以做到这一点......编辑......

如果您的 Orders 表有一个 SupplierID 列,那么

DECLARE @firstdate DATETIME='19970101 00:00:00', 
        @lastdate DATETIME= '19970131 00:00:00'
DECLARE @datetable TABLE (auxDate DATE)

;WITH CTE_DatesTable
  AS
  (
    SELECT @FirstDate AS auxDate
    UNION ALL
    SELECT DATEADD(dd, 1, auxDate)
    FROM CTE_DatesTable
    WHERE DATEADD(dd, 1, auxDate) <= @LastDate
  )
  INSERT INTO @datetable ([auxDate])
  SELECT [auxDate] FROM CTE_DatesTable
  OPTION (MAXRECURSION 0);

SELECT auxDate AS 'HavntRecivedOrder'
      FROM @datetable auxTB
     WHERE DAY(auxTB.auxDate) NOT IN (SELECT DAY(OrderDate)
                                   FROM Orders, Supplier
                                  WHERE OrderDate = BETWEEN @firstdate AND @lastdate
                                    AND Supplier.SupplierID = Orders.SupplierID)
于 2012-07-25T09:20:24.350 回答