Plsql 搜索符合多个条件的数据
试图提取数据库中符合特定条件的人员。他们需要x&y、x&z或x&w 我尝试过使用“in”和“or”,但我得到了错误或糟糕的结果(有些只显示x,而不是其中的两个) 我得到了一份每个人的名单,似乎很多人只有X的代码,而不是X&这是其他因素之一。像这样的吗Plsql 搜索符合多个条件的数据,plsql,Plsql,试图提取数据库中符合特定条件的人员。他们需要x&y、x&z或x&w 我尝试过使用“in”和“or”,但我得到了错误或糟糕的结果(有些只显示x,而不是其中的两个) 我得到了一份每个人的名单,似乎很多人只有X的代码,而不是X&这是其他因素之一。像这样的吗 SQL> with part (name, code) as 2 (select 'Mike', 'x' from dual union all -- Mike is OK, has X and Y 3 select
SQL> with part (name, code) as
2 (select 'Mike', 'x' from dual union all -- Mike is OK, has X and Y
3 select 'Mike', 'y' from dual union all
4 select 'Mike', 'a' from dual union all
5 --
6 select 'Rita', 'a' from dual union all -- Rita is not OK, has only X
7 select 'Rita', 'x' from dual union all
8 --
9 select 'John', 'x' from dual union all -- John is OK, has X and W
10 select 'John', 'w' from dual union all
11 --
12 select 'Anna', 'z' from dual -- Anna is not OK, has only Z
13 )
14 select name
15 From part
16 where code in ('x', 'y', 'z', 'w')
17 group by name
18 having count(*) = 2;
NAME
----
John
Mike
SQL>
像这样的
SQL> with part (name, code) as
2 (select 'Mike', 'x' from dual union all -- Mike is OK, has X and Y
3 select 'Mike', 'y' from dual union all
4 select 'Mike', 'a' from dual union all
5 --
6 select 'Rita', 'a' from dual union all -- Rita is not OK, has only X
7 select 'Rita', 'x' from dual union all
8 --
9 select 'John', 'x' from dual union all -- John is OK, has X and W
10 select 'John', 'w' from dual union all
11 --
12 select 'Anna', 'z' from dual -- Anna is not OK, has only Z
13 )
14 select name
15 From part
16 where code in ('x', 'y', 'z', 'w')
17 group by name
18 having count(*) = 2;
NAME
----
John
Mike
SQL>
@Littlefoot的替代品。它使用EXISTS for代替group和having子句。我增加了两个额外的测试:朱迪和所有的“w”、“x”和“y”以及缺少x的彼得。根据这些条件是合格还是不合格,查询会有所不同
with part (name, code) as
( select 'Mike', 'x' from dual union all -- Mike is OK, has X and Y
select 'Mike', 'y' from dual union all
select 'Mike', 'a' from dual union all
--
select 'Rita', 'a' from dual union all -- Rita is not OK, has only X
select 'Rita', 'x' from dual union all
--
select 'Judy', 'x' from dual union all -- Judy is OK, has W, X and Y
select 'Judy', 'y' from dual union all
select 'Judy', 'w' from dual union all
--
select 'Peter', 'y' from dual union all --Peter is not OK, does not have X
select 'Peter', 'w' from dual union all
--
select 'John', 'x' from dual union all -- John is OK, has X and W
select 'John', 'w' from dual union all
--
select 'Anna', 'z' from dual -- Anna is not OK, has only Z
)
select p1.name
from part p1
where 1=1
and p1.code = 'x'
and exists (select null
from part p2
where 1=1
and p2.name = p1.name
and p2.code in ('y', 'z', 'w')
);
@Littlefoot的替代品。它使用EXISTS for代替group和having子句。我增加了两个额外的测试:朱迪和所有的“w”、“x”和“y”以及缺少x的彼得。根据这些条件是合格还是不合格,查询会有所不同
with part (name, code) as
( select 'Mike', 'x' from dual union all -- Mike is OK, has X and Y
select 'Mike', 'y' from dual union all
select 'Mike', 'a' from dual union all
--
select 'Rita', 'a' from dual union all -- Rita is not OK, has only X
select 'Rita', 'x' from dual union all
--
select 'Judy', 'x' from dual union all -- Judy is OK, has W, X and Y
select 'Judy', 'y' from dual union all
select 'Judy', 'w' from dual union all
--
select 'Peter', 'y' from dual union all --Peter is not OK, does not have X
select 'Peter', 'w' from dual union all
--
select 'John', 'x' from dual union all -- John is OK, has X and W
select 'John', 'w' from dual union all
--
select 'Anna', 'z' from dual -- Anna is not OK, has only Z
)
select p1.name
from part p1
where 1=1
and p1.code = 'x'
and exists (select null
from part p2
where 1=1
and p2.name = p1.name
and p2.code in ('y', 'z', 'w')
);
有没有一种不用特定人搜索的方法?将有超过一千或两千个结果,如果有解决办法,我不想输入那么多数据。我认为你误解了CTE的目的。在这种情况下,它严格用作测试数据生成器,以验证实际查询。在函数查询中,您将删除CTE并用实际表名替换“part”。该表可能包含ID等其他列。由于您没有提供该信息,只提供名称和代码,因此我们被迫使用名称。没有搜索特定名称;它只是一个用于比较和分组的鉴别器。谢谢@Belayer。在这个表(零件表)中,我确实有一个ID,每个人都有不同的ID。还有一个参与者代码,每个人可以有一个或最多3个或4个。这些参与者代码适用于该人员所属的各种组。我只想找到属于多个组的人。我不确定我是否理解如何删除CTE并用实际的表名替换part,因为表名是part(针对参与者)。感谢您的帮助。表名为“part”。我在阅读原始问题时一定错过了,对此我深表歉意。所以只需删除CTE本身。也可以用ID替换列名。有没有不使用特定人员进行搜索的方法?将有超过一千或两千个结果,如果有解决办法,我不想输入那么多数据。我认为你误解了CTE的目的。在这种情况下,它严格用作测试数据生成器,以验证实际查询。在函数查询中,您将删除CTE并用实际表名替换“part”。该表可能包含ID等其他列。由于您没有提供该信息,只提供名称和代码,因此我们被迫使用名称。没有搜索特定名称;它只是一个用于比较和分组的鉴别器。谢谢@Belayer。在这个表(零件表)中,我确实有一个ID,每个人都有不同的ID。还有一个参与者代码,每个人可以有一个或最多3个或4个。这些参与者代码适用于该人员所属的各种组。我只想找到属于多个组的人。我不确定我是否理解如何删除CTE并用实际的表名替换part,因为表名是part(针对参与者)。感谢您的帮助。表名为“part”。我在阅读原始问题时一定错过了,对此我深表歉意。所以只需删除CTE本身。还将列名替换为ID。