我是DB2 dveloper的初学者
我要求通过加入两个表来显示下面的输出,以显示排除状态的结果。 enter image description here
水果清单
Name Type
---- ----
apple fruit
cranberry fruit
blueberry fruit
neamleaf leaf
lemongrass leaf
过滤器:
Name Type
---- ----
%berry fruit
%grass leaf
%root vegetable
过滤结果,显示与like
过滤器列表不匹配的水果
Name Type
---- ----
apple fruit
neamleaf leaf
任何想法如何获得它?
答案 0 :(得分:1)
我试过了:
create table master
(type varchar(10),
name varchar(25));
insert into master
values ('fruit', 'apple'),
('fruit', 'pineapple'),
('fruit', 'cranberry'),
('fruit', 'blueberry'),
('fruit', 'papaya'),
('fruit', 'orange'),
('leaf', 'neamleaf'),
('leaf', 'lemongrass'),
('leaf', 'orangegrass');
create table slave
(type varchar(10),
search varchar(25));
insert into slave
values ('fruit', 'pple'),
('fruit', 'berry'),
('leaf', 'grass');
select m.type, m.name
from master m
where not exists
(select *
from slave s
where s.type = m.type
and m.name like '%' || s.search || '%');
它提供了以下输出:
type name
----- --------
fruit papaya
fruit orange
leaf neamleaf
<强>为什么强>
您描绘的输出让我感到困惑,因为我不希望您的查询首先工作。连接对我来说不起作用(也许该语法特定于LUW,但由于select
拼写为slect
,我怀疑你的例子是手工键控而不是剪切和粘贴)。当我尝试使用适用于我的语法的方法时,我得到了两行用于木瓜,两行用于橙色,另外一行用于其他每个水果。这是因为使用m.type = s.type
的联接为每个水果提供了两行(slave
中每个'水果'行一个),然后m.name not like '%'||s.search||'%'
过滤掉不匹配的行结果集中的搜索字段。
让我们看看未经过滤的结果集:
m.type m.name s.type s.search filtered/output
------ ----------- --------- ----------- ---------------
fruit apple fruit pple xxxx filtered
fruit apple fruit berry output
fruit pineapple fruit pple xxxx filtered
fruit pineapple fruit berry output
fruit cranberry fruit pple output
fruit cranberry fruit berry xxxx filtered
fruit blueberry fruit pple output
fruit blueberry fruit berry xxxx filtered
fruit papaya fruit pple output
fruit papaya fruit berry output
fruit orange fruit pple output
fruit orange fruit berry output
leaf neamleaf leaf grass output
fruit lemongrass leaf grass xxxx filtered
fruit orangegrass leaf grass xxxx filtered