0

好的,这是我的数据库提取

customers- ID, Fname, LName, Username, Password
Products - ProductsID, Name
Requests - requestid, productsid[fk for products table], cid[fk for customers table],
quantity, matchType
sellers - saleid, productsid[fk for products table], cid[fk for customers table], quantity
price.

所以它们是我的带有 FK 表示外键的表......现在我遇到的问题是说这是从表值中提取的

customers=1,John,Smith,js123,123
customers=2,Joe,bloggs,jb555,abc
products=1,Sweets
products=2, chocolate
requests=1, 2, 1, 200, Exact
requests=2, 1, 1, 50, HighQuality
sellers =1,2,2,200,5.00

好的,所以这里发生的事情是我们有 2 个客户和 2 个产品(糖果和巧克力),请求表显示.. requestid [主键],产品 id,请求产品的客户 id,以克为单位的数量以及他们希望如何显示匹配项。因此,如果我们接受第一个请求,它将如下所示... REquestID 1[pk],想要 chcolates(2),想要巧克力的用户是 john smith,他想要与他的请求完全匹配的 200 克价值。 .. 现在我们有一个卖家,即 joe bloggs,他以 5.00 英镑的价格出售 200 克的巧克力......现在我遇到的问题是如何运行一个查询,将该卖家与相应的客户相匹配。即根据客户匹配类型(pre-fences)列出卖家名称这是我尝试过的:

select c.Fname, c.Lname, p.Name FROM customers c, Products p, Requests, sellers 
WHERE c.ID=sellers.cid AND p.ProductsID=sellers.productsid 

这将列出所有卖家名称和他们销售的产品名称。但是,我希望只显示请求表中单词为 EXACT 的那些产品。因此只匹配确切的数量。所以,我尝试了:

select c.Fname, c.Lname, p.Name FROM customers c, Products p, Requests, sellers 
WHERE c.ID=sellers.cid AND p.ProductsID=sellers.productsid  
AND Requests.productsid=sellers.productid AND Requests.matchType ='Exact'

我已经尝试过了,但这并不能正确输出我想要的。我该如何解决它我希望我的输出是:

joe,bloggs,choclates

所以这匹配requestid 1 where。但是我无法得到这个输出,有人能帮我吗?当然,通过将 products.id 与 Sellers.productsid 匹配会显示名称,然后通过将 requests.productid 与 Seller.productid 匹配 WHERE requests.matchType='Exact' 它应该可以工作吗?但它没有显示我期望的输出。

4

1 回答 1

0

当 matchType 准确时,request.Quantity = Sellers.quantity。所以我想你的 where 子句应该是

WHERE c.ID=sellers.cid 
  AND p.ProductsID=sellers.productsid  
  AND Requests.productsid=sellers.productid 
  AND CASE WHEN Requests.matchType ='Exact' THEN sellers.quantity
           ELSE -1 END = CASE WHEN Requests.matchType ='Exact' THEN Requests.quantity
                              ELSE -1 END

或者

WHERE c.ID=sellers.cid 
  AND p.ProductsID=sellers.productsid  
  AND Requests.productsid=sellers.productid 
  AND ((Requests.matchType ='Exact' AND sellers.quantity = Requests.quantity)
       OR Requests.matchType != 'Exact') 
于 2013-02-11T22:38:51.500 回答