Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在Oracle中的表中查找特定值?_Sql_Oracle_Plsql_Plsqldeveloper - Fatal编程技术网

Sql 如何在Oracle中的表中查找特定值?

Sql 如何在Oracle中的表中查找特定值?,sql,oracle,plsql,plsqldeveloper,Sql,Oracle,Plsql,Plsqldeveloper,假设我的表是TEST_123,它有以下记录: id | cid | result ------------------ 1 | C-1 | TAM 2 | C-1 | TAM 3 | C-2 | RAM 4 | C-2 | TAM 5 | C-3 | SAM 6 | C-3 | SAM 现在我想要这样的cid,它只有一种结果,所以答案应该是C-1和C-3,而不是C-2,因为它有两种不同类型的结果。需要Oracle查询吗?使用存在,这有点棘手,因

假设我的表是TEST_123,它有以下记录:

id |  cid | result
------------------
1  |  C-1 |   TAM
2  |  C-1 |   TAM
3  |  C-2 |   RAM
4  |  C-2 |   TAM
5  |  C-3 |   SAM
6  |  C-3 |   SAM

现在我想要这样的cid,它只有一种结果,所以答案应该是C-1和C-3,而不是C-2,因为它有两种不同类型的结果。需要Oracle查询吗?

使用存在,这有点棘手,因为每个组的结果应该是相同的

 select t1.* from TEST_123 t1 where exists(
             select 1 from TEST_123 t2 where t2.cid=t1.cid
                                      and t2.result=t1.result
                                      group by t2.cid,t2.result
                                      having count(*)=
                                       (select count(*) from TEST_123 t3
                                       where t3.cid=t2.cid)
                                      )

with TEST_123 as
(
select 1 as id , 'c-1' as cid , 'tam' as result from dual
union all
select 2 as id , 'c-1' as cid , 'tam' as result from dual
union all
select 3 as id , 'c-2' as cid , 'tam' as result from dual
union all
select 4 as id , 'c-2' as cid , 'ram' as result from dual

)

select distinct t1.cid from TEST_123 t1 where exists(
                 select 1 from TEST_123 t2 where t2.cid=t1.cid
                                          and t2.result=t1.result
                                          group by t2.cid,t2.result
                                          having count(*)=
                                           (select count(*) from TEST_123 t3
                                           where t3.cid=t2.cid)
                                          )

使用存在,这有点棘手,因为每个小组的结果应该是相同的

 select t1.* from TEST_123 t1 where exists(
             select 1 from TEST_123 t2 where t2.cid=t1.cid
                                      and t2.result=t1.result
                                      group by t2.cid,t2.result
                                      having count(*)=
                                       (select count(*) from TEST_123 t3
                                       where t3.cid=t2.cid)
                                      )

with TEST_123 as
(
select 1 as id , 'c-1' as cid , 'tam' as result from dual
union all
select 2 as id , 'c-1' as cid , 'tam' as result from dual
union all
select 3 as id , 'c-2' as cid , 'tam' as result from dual
union all
select 4 as id , 'c-2' as cid , 'ram' as result from dual

)

select distinct t1.cid from TEST_123 t1 where exists(
                 select 1 from TEST_123 t2 where t2.cid=t1.cid
                                          and t2.result=t1.result
                                          group by t2.cid,t2.result
                                          having count(*)=
                                           (select count(*) from TEST_123 t3
                                           where t3.cid=t2.cid)
                                          )

根据@zaynul的回答,这里是另一个变体:

with TEST_123 as
(
select 1 as id , 'c-1' as cid , 'tam' as result from dual
union all
select 2 as id , 'c-1' as cid , 'tam' as result from dual
union all
select 3 as id , 'c-2' as cid , 'tam' as result from dual
union all
select 4 as id , 'c-2' as cid , 'ram' as result from dual
)
select * from test_123 where cid in (
    select cid from test_123 group by cid having count(distinct result) = 1);

根据@zaynul的回答,这里是另一个变体:

with TEST_123 as
(
select 1 as id , 'c-1' as cid , 'tam' as result from dual
union all
select 2 as id , 'c-1' as cid , 'tam' as result from dual
union all
select 3 as id , 'c-2' as cid , 'tam' as result from dual
union all
select 4 as id , 'c-2' as cid , 'ram' as result from dual
)
select * from test_123 where cid in (
    select cid from test_123 group by cid having count(distinct result) = 1);

您只需要理解GROUPBY和HAVING子句

答案很简单

select cid
from TEST_123
group by cid
having count(distinct result) = 1

注释group by从CID中选择不同的键;having filters on条件对组中的所有记录都有效,在您的情况下countdistinct result=1

您只需要理解group BY和having子句

答案很简单

select cid
from TEST_123
group by cid
having count(distinct result) = 1
注释group by从CID中选择不同的键;having filters on条件对组中的所有记录都有效,在您的情况下countdistinct result=1

我应该为你工作

应该为您工作

我将使用不存在的:

我将使用不存在的:


欢迎来到堆栈溢出!问问题时请更具体一点:到目前为止,您用代码示例做了哪些尝试?/你期待什么你有什么错误?有关帮助,请查看欢迎使用堆栈溢出!问问题时请更具体一点:到目前为止,您用代码示例做了哪些尝试?/你期待什么你有什么错误?如需帮助,请查看“谢谢Marmite…”。。虽然它很好地回答了我的问题,但我被困在TEST_123组中的select cid上,因为cid的COUNTRUST=1,并没有将其放入distinct。。。但有一个疑问,当我为result=TAM放入where子句时,它给出了C1和C2,但我真正想要的只是C1,你能理解我吗?如果你过滤where only TAM,那么所有剩余的组每个定义只有一个键TAM,所以结果中的C2是可以的。如果您正在寻找一个只有一种结果类型(即TAM)的键,则必须将countdistinct result=1和maxresult='TAM'公式化,您可以很好地使用MINas,因为结果只有一个值。谢谢Marmite。。虽然它很好地回答了我的问题,但我被困在TEST_123组中的select cid上,因为cid的COUNTRUST=1,并没有将其放入distinct。。。但有一个疑问,当我为result=TAM放入where子句时,它给出了C1和C2,但我真正想要的只是C1,你能理解我吗?如果你过滤where only TAM,那么所有剩余的组每个定义只有一个键TAM,所以结果中的C2是可以的。如果您正在寻找一个只有一种结果类型(即TAM)的键,则必须将countdistinct result=1和maxresult='TAM'公式化,您可以很好地使用MINas,因为结果只有一个值。