Sql 根据其他行中的值进行选择

时间:2021-04-21 12:52:11

标签: sql oracle select rows oracle12c

我有一张如下表:

DS_no   Value   Language   Nach
1        0        EN        123
2        ABC      EN        123
2        DCEF     EN        123
1        1        EN        122
2        XZU      EN        122
2        SDF      EN        122

所以我希望我的 select 语句检查 DS_no 的值是否为每个 nach 为 1。如果值为 1,则应为该 nach 选择所有其他值。例如。 nach 122 的 ds_no 1。如果 ds_no 1 的值为 0,则选择不应返回任何值。

例如:nach 123 的 ds_no 1 值为 0。所以我不想选择 nach 123 的任何值。但 nach 122 的 ds_no 1 值为 1。所以我想选择 nach 的所有值122 除了值 1。

2 个答案:

答案 0 :(得分:1)

没有 first 值。但是您可以使用 0:

检查 any 值是否为 exists
select t.*
from t
where exists (select 1
              from t t2
              where t2.nach = t.nach and t2.value = '0'
             );

答案 1 :(得分:0)

发布时的示例数据

SQL> select * from test;

     DS_NO VALU       NACH
---------- ---- ----------
         1 0           123
         2 abc         123
         2 dcef        123
         1 1           122
         2 xzu         122
         2 sdf         122

6 rows selected.

返回您描述的内容的查询,附带说明您 - 很可能 - 在此处输入了错误:

<块引用>

所以我想选择 nach 123 除了值 1 之外的所有值。

123 应该是 122,我想。

SQL> select ds_no, value, nach
  2  from test a
  3  where value <> '1'
  4    and exists (select null
  5                from test b
  6                where b.nach = a.nach
  7                  and b.value = '1'
  8               );

     DS_NO VALU       NACH
---------- ---- ----------
         2 sdf         122
         2 xzu         122

SQL>
相关问题