SQL多次选择多个条件

时间:2015-02-18 23:07:09

标签: sql sql-server

我需要从Grid表中选择与连接表中的多个条件匹配的所有信息,即。

Select * from Grid
join table1...table2...table3....table4
where (table3.product = 'product1' AND table2.rate = 2) 
AND (table3.product = 'product2' AND table2.rate = 3 AND table2.lage = 75) 
AND (table3.product = 'product3' AND table2.rate = 0.25 AND table2.lage = 75 AND table2.uage=85)

这是我的实际选择查询:

Select * from Grid gr  
join GridToPage g2p on gr.[GridID] = g2p.[GridID] 
join Page p on p.[PageID] = g2p.[PageID] 
join PageToProduct p2p on p2p.[PageID] = p.[PageID] 
join Product pr on p2p.[ClumpCode] = pr.[ClumpCode] 
join PageToProductToRate p2p2r on p2p2r.[PageToProductID] = p2p.[PageToProductID] 
join ProductRates r on r.[ProductRatesID] = p2p2r.[ProductRatesID] 
where 
(pr.[ClumpCode] ='CUST5G' AND r.[Rate]=2 AND r.[LowerAge] = 0 AND r.[UpperAge] = 75)
AND (pr.[ClumpCode] = 'CUST7' AND r.[Rate] = 4)
AND (pr.[ClumpCode] = 'CUST5' AND r.[LowerAge] = 75 AND r.[UpperAge] = 85)

可能有多个' AND'条件。它将返回满足所有条件的所有记录。这个查询只有一行条件可以正常工作,但不止一个它没有返回任何结果。 pr。[ClumpCode] 是必需条件,其他则不是,并且特定[ClumpCode]可能还有4个条件。下一行条件适用于其他[ClumpCode]等。 我也尝试过使用' HAVING'每行的总和> 0而不是' WHERE(...)AND'但它给了我一些错误。 当我有多个' AND'当我有多个' AND'条件?

------------ EDIT ----------------------- 感谢您的评论,实际上[ClumpCode]上的条件应该来自Product表而不是ProductRate,因为我有它。 在上面的查询中进行了更正。它必须是' AND'并满足所有条件返回真实。

---------- EDIT2 ------------------ 图像说明搜索将如何为“ML_GRID”带来结果。 enter image description here

4 个答案:

答案 0 :(得分:2)

这将始终返回false,因为r不能同时拥有多个[ClumpCode][Rate][LowerAge][UpperAge]

(r.[ClumpCode] ='CUST5G' AND r.[Rate]=2 AND r.[LowerAge] = 0 AND r.[UpperAge] = 75)
AND ( r.[ClumpCode] = 'CUST7' AND r.[Rate] = 4)
AND ( r.[ClumpCode] = 'CUST5' AND r.[LowerAge] = 75 AND r.[UpperAge] = 85)

您需要在您的括号条件之间使用OR而不是AND

答案 1 :(得分:1)

单个 [ClumpCode] 不能有不同的值,您需要 OR 条件。这通常使用GROUP BY / HAVING完成,将条件移动到Derived Table中,如下所示:

join
 ( SELECT [ProductRatesID]
   FROM ProductRates 
   where 
      (r.[ClumpCode] ='CUST5G' AND r.[Rate]=2 AND r.[LowerAge] = 0 AND r.[UpperAge] = 75)
      OR ( r.[ClumpCode] = 'CUST7' AND r.[Rate] = 4)
      OR ( r.[ClumpCode] = 'CUST5' AND r.[LowerAge] = 75 AND r.[UpperAge] = 85)
   GROUP BY [ProductRatesID]
   HAVING COUNT(*) = 3 -- the number of different conditions in WHERE
 ) as r
on r.[ProductRatesID] = p2p2r.[ProductRatesID] 

这假设每个条件只有一行。否则你可能需要改为 COUNT(DISTINCT ???)

答案 2 :(得分:0)

上述所有答案都没有解决我的问题。如果我使用' OR'在条件之间,它将返回所有提供一个或多个条件的结果,其中我只需要满足所有条件的匹配。 在每个条件的查询之间使用INTERSECT结束,并仅返回Grid表的列与选择all。不确定这是最有效的方法。

Select gr.GridID from Grid gr  
join GridToPage g2p on gr.[GridID] = g2p.[GridID] 
join Page p on p.[PageID] = g2p.[PageID] 
join PageToProduct p2p on p2p.[PageID] = p.[PageID] 
join Product pr on p2p.[ClumpCode] = pr.[ClumpCode] 
join PageToProductToRate p2p2r on p2p2r.[PageToProductID] = p2p.[PageToProductID] 
join ProductRates r on r.[ProductRatesID] = p2p2r.[ProductRatesID] 
where 
(pr.[ClumpCode] ='CUST5G' AND r.[Rate]=2 AND r.[LowerAge] = 0 AND r.[UpperAge] = 75) --condition #1
INTERSECT
Select gr.GridID from Grid gr  
join GridToPage g2p on gr.[GridID] = g2p.[GridID] 
join Page p on p.[PageID] = g2p.[PageID] 
join PageToProduct p2p on p2p.[PageID] = p.[PageID] 
join Product pr on p2p.[ClumpCode] = pr.[ClumpCode] 
join PageToProductToRate p2p2r on p2p2r.[PageToProductID] = p2p.[PageToProductID] 
join ProductRates r on r.[ProductRatesID] = p2p2r.[ProductRatesID] 
WHERE (pr.[ClumpCode] = 'CUST7' AND r.[Rate] = 4) --condition #2
INTERSECT
....

答案 3 :(得分:0)

您的查询的这部分是奇怪的:

join Product pr on p2p.ClumpCode = pr.ClumpCode
join PageToProductToRate p2p2r on p2p2r.PageToProductID = p2p.PageToProductID
join ProductRates r on r.ProductRatesID = p2p2r.ProductRatesID

我们都看了你的图片,并期望以这种方式组织数据,遵循早期联接的模式:

join Product pr on p2p.ClumpCode = pr.ClumpCode
join ProductToRate p2r on p2r.ProductID = pr.ProductID
join ProductRates r on r.RateID = p2r.RateID

所以我怀疑这些表格之间的关系并不是你所描述的,或者至少是我们读者从我们自己的假设中得到的印象。也许最简单的问题是为什么有PageToProductToRate表而不是ProductToRate表?

如果你说你有UNION工作但是你告诉我们你INTERSECT为你工作而且只会加深这个谜团,我们都不会感到惊讶。我真的好奇地看到这里发生了什么,而我的直觉仍然说非常非常错误。

我猜测如果您运行下面的查询,您将获得超过11行(这是您的图片显示的数字)以及更多列。如果您愿意继续分享,我想尝试解决这个问题。

select *
from
    Grid gr  
    inner join GridToPage g2p on gr.GridID = g2p.GridID
    inner join Page p on p.PageID = g2p.PageID 
    inner join PageToProduct p2p on p2p.PageID = p.PageID
    inner join Product pr on p2p.ClumpCode = pr.ClumpCode
    inner join PageToProductToRate p2p2r on p2p2r.PageToProductID = p2p.PageToProductID
    inner join ProductRates r on r.[ProductRatesID] = p2p2r.[ProductRatesID] 
where
    GridID = 'ML_GRID' /* or whatever is the appropriate value here */