1

我创建了一个查询,允许我使用以下代码查看公司名称 = Best Baths 的客户订购的所有商品。

Select co.OrderID, cu.FName + '  ' + cu.SName as 'Name', 
cu.Address1 + ', ' + cu.Address2 + ', ' + cu.Address3 as 'Dispatch Address', 
cu.PostCode, 
ma.MaterialName as 'Item',
mi.Price as 'Item Price',
co.DateOrdered as 'Order Date',
pm.DateReceived,

CASE WHEN la.Status = 'Blocked' THEN 'Blocked' ELSE 'Active' END AS Status

from Customers cu
-- inner join the following to find customers orders and cost
inner join CustomerOrder co on co.CustomerID = cu.CustomerID
inner join ItemOrder io on co.ID = io.ItemOrderID
inner join materialItem mi on io.MaterialID = mi.MaterialItemID
inner join Material ma on io.MaterialID = ma.MaterialItemID
left join ItemForInvoice ifi on io.ItemOrderID = ifi.ItemOrderID
left join Invoice iv on ifi.InvoiceItemID = iv.InvoiceItemID
left join PaymentMethod pm on iv.InvoiceID = pm.InvoiceID
left join LockedAccount la on pm.PaymentID = la.PaymentID
inner join Suppliers su on mi.SupplierID = su.SuppliersID
inner join SupplierDetails sd on su.SuppliersID = sd.SuppliersID
Where su.SuppliersName = 'Best Baths'

我现在想创建一个显示尚未订购的商品的查询,有人能指出我正确的方向吗?

4

3 回答 3

2

从您的查询中不清楚什么表是什么,但基本查询类似于

SELECT items.id, COUNT(orders.itemid) AS cnt
FROM items
LEFT JOIN orders ON items.id = orders.itemid
GROUP BY items.id
HAVING (cnt = 0)
于 2012-11-27T20:44:37.457 回答
2

Annoscia,看这个例子:

(SELECT coll FROM TableA)
EXCEPT
(SELECT coll FROM TableB)

意思是:

SELECT DISTINCT coll
  FROM TableA 
 WHERE NOT EXISTS (SELECT * 
                     FROM TableB 
                    WHERE TableA.col1 = TableB.col1)

对于你的情况,它会是这样的:

Select co.OrderID, cu.FName + '  ' + cu.SName as 'Name', 
cu.Address1 + ', ' + cu.Address2 + ', ' + cu.Address3 as 'Dispatch Address', 
cu.PostCode, 
ma.MaterialName as 'Item',
mi.Price as 'Item Price',
co.DateOrdered as 'Order Date',
pm.DateReceived,    
CASE WHEN la.Status = 'Blocked' THEN 'Blocked' ELSE 'Active' END AS Status

from Customers cu
-- inner join the following to find customers orders and cost
inner join CustomerOrder co on co.CustomerID = cu.CustomerID
inner join ItemOrder io on co.ID = io.ItemOrderID
inner join materialItem mi on io.MaterialID = mi.MaterialItemID
inner join Material ma on io.MaterialID = ma.MaterialItemID
left join ItemForInvoice ifi on io.ItemOrderID = ifi.ItemOrderID
left join Invoice iv on ifi.InvoiceItemID = iv.InvoiceItemID
left join PaymentMethod pm on iv.InvoiceID = pm.InvoiceID
left join LockedAccount la on pm.PaymentID = la.PaymentID
inner join Suppliers su on mi.SupplierID = su.SuppliersID
inner join SupplierDetails sd on su.SuppliersID = sd.SuppliersID

EXCEPT

Select co.OrderID, cu.FName + '  ' + cu.SName as 'Name', 
cu.Address1 + ', ' + cu.Address2 + ', ' + cu.Address3 as 'Dispatch Address', 
cu.PostCode, 
ma.MaterialName as 'Item',
mi.Price as 'Item Price',
co.DateOrdered as 'Order Date',
pm.DateReceived,

CASE WHEN la.Status = 'Blocked' THEN 'Blocked' ELSE 'Active' END AS Status

from Customers cu
-- inner join the following to find customers orders and cost
inner join CustomerOrder co on co.CustomerID = cu.CustomerID
inner join ItemOrder io on co.ID = io.ItemOrderID
inner join materialItem mi on io.MaterialID = mi.MaterialItemID
inner join Material ma on io.MaterialID = ma.MaterialItemID
left join ItemForInvoice ifi on io.ItemOrderID = ifi.ItemOrderID
left join Invoice iv on ifi.InvoiceItemID = iv.InvoiceItemID
left join PaymentMethod pm on iv.InvoiceID = pm.InvoiceID
left join LockedAccount la on pm.PaymentID = la.PaymentID
inner join Suppliers su on mi.SupplierID = su.SuppliersID
inner join SupplierDetails sd on su.SuppliersID = sd.SuppliersID
Where su.SuppliersName = 'Best Baths'
于 2012-11-28T08:41:39.237 回答
1

如果您正在寻找尚未订购的材料(即项目),那么 NOT EXISTS 子句将很有效。您只需要通过 ItemOrder 表检查它是否没有参与到订单中。

Select ma.MaterialName as [Item],
       mi.Price as [Item Price]
  from Material ma
  join materialItem mi on io.MaterialID = mi.MaterialItemID
 where not exists (select *
                     from ItemOrder io
                    where io.MaterialID = ma.MaterialItemID)

以后请不要使用 ( ') 单引号引用列别名,请使用方括号。

于 2012-11-27T21:12:57.210 回答