Sql 有没有办法从DB2中where子句中的条件集获得结果

Sql 有没有办法从DB2中where子句中的条件集获得结果,sql,db2,where-clause,Sql,Db2,Where Clause,我有通常的衬衫库存表:尺寸、颜色和品牌。我必须找到匹配的价格并添加到电子表格的列表中 我对[大号,红色,耐克],[小号,白色,美洲狮]和[中号,蓝色,H&M]感兴趣 我现在正在做的是: select size,colour,brand,price from inventory where size in ('large','small','medium') and colour in ('red','white','blue') and brand in ('Nike','Puma','H&a

我有通常的衬衫库存表:尺寸、颜色和品牌。我必须找到匹配的价格并添加到电子表格的列表中

我对[大号,红色,耐克],[小号,白色,美洲狮]和[中号,蓝色,H&M]感兴趣

我现在正在做的是:

select size,colour,brand,price from inventory 
where size in ('large','small','medium')
and colour in ('red','white','blue')
and brand in ('Nike','Puma','H&M')
然后我会得到一个长长的清单,上面列出了满足3*3条件的任意组合的库存。然后我会做一个xlookup来找到我需要的三个精确的组合

(我的实际列表显然要长得多,并且有更多的变量,这些变量有更多的值。但这是最简单的示例)

这显然是非常无效的,但我不知道如何使我的查询更直接

有没有办法制作一个简单的循环,比如说,循环一系列条件?差不多

condA=('large','small','medium')
condB= ('red','white','blue')
condC=('Nike','Puma','H&M')
for a = 0 to 2
select size, colour,brand,price from inventory 
where size=condA(a)
and colour=condB(a)
and brand=condC(a)
next a

我使用的是一个DB2数据库,如果这有什么不同的话…

最好的方法是直接使用您要查找的精确组合过滤案例:

select size,colour,brand,price from inventory 
where (size ='large' and brand = 'Nike' and colour='red')
or ( size ='small' and brand = 'Puma' and colour='white') 
or (size ='medium' and brand = 'H&M' and colour='blue')
这样,您不需要查看提取的结果以确保组合正确

只需为每个属性分配一个“组号”,并在
WHERE
子句中使用这样的组号即可。优点是,您不必指定一长串OR'ed谓词。如果愿意,只需为每个属性类型创建3个表引用就足够了

在下面的示例中,组合:
[大号,红色,耐克]获得组号1
[small,white,Puma]获得组号2
[中号,蓝色,H&M]获得组号3

select 
  inventory.size
, inventory.colour
, inventory.brand
, inventory.price 
from 
(
values 
  ('large', 'red', 'Nike', 1)
, ('small', 'white', 'Puma', 1)
, ('medium', 'blue', 'H&M', 1)
-- any other combinations like below are not returned
, ('large', 'red', 'Puma', 1)
, ('small', 'white', 'Nike', 1)
) inventory (size, colour, brand, price)
join
(
values ('large', 1), ('small', 2), ('medium', 3)
) sizes (size, id) on sizes.size = inventory.size
join
(
values ('red', 1), ('white', 2), ('blue', 3)
) colours (colour, id) on colours.colour = inventory.colour
join
(
values ('Nike', 1), ('Puma', 2), ('H&M', 3)
) brands (brand, id) on brands.brand = inventory.brand
where sizes.id = colours.id and colours.id = brands.id;
(尺寸、颜色、品牌)在((‘大’、‘红’、‘耐克’、…)中的位置?