【问题标题】:Creating required script创建所需的脚本
【发布时间】:2020-09-17 15:15:41
【问题描述】:

我的问题是关于 select 语句。我有一个名为 test1 的表及其中的值。这是我用于创建表和插入值的脚本:

create table test1(id number, pc number, pe number);
insert into test1 values(12,0,900); 
insert into test1 values(12,0,901);
insert into test1 values(12,0,902);
insert into test1 values(12,91,900);
insert into test1 values(12,0,1);
insert into test1 values(12,91,900); 
insert into test1 values(12,91,901); 
insert into test1 values(12,91,900);
insert into test1 values(12,91,5);
insert into test1 values(13,0,900);
insert into test1 values(12,0,20); 
insert into test1 values(12,1,1);
insert into test1 values(12,0,900);
insert into test1 values(13,91,900); 
insert into test1 values(13,91,901); 
insert into test1 values(13,91,902); 
insert into test1 values(13,0,902);
insert into test1 values(13,91,201); 
insert into test1 values(13,91,202);
insert into test1 values(13,91,20);
insert into test1 values(13,0,900);
insert into test1 values(13,0,900); 
commit;

我的问题是如何从 test1 表中选择 pc 列仅包含 0 或 91Ids /strong> 和 pe 列仅包含 20、201、202、900、901 或 902。所以Id就像13

【问题讨论】:

    标签: sql oracle sql-scripts


    【解决方案1】:

    只需检查满足行数是否等于 count(*):

    select id
    from test1
    group by id
    having 
       count(*) = count(case when pc in (0,91) and pe in (20, 201, 202, 900, 901, 902) then 1 end)
    

    【讨论】:

    • 谢谢,它成功了,但我真的不明白。请你解释一下?
    • 哎呀我明白了@Sayan Malakshinov。非常感谢。
    【解决方案2】:

    这是给你的 sql

    select id from test1 where pc in (0,91) and pe in (20, 201, 202, 900, 901, 902);
    

    【讨论】:

    • 没有兄弟。如果您不知道确切的 ID 怎么办?在你的情况下,如果我只给出 where id = 13 就足够了,即使你没有指定 pc 和 pe 列。
    • 试试这个 select id from test1 where pc in (0,91) and pe in (20, 201, 202, 900, 901, 902)
    • 您的第二条评论也不正确,它会给我们输出 id = 12,因为在 test1 表中也有一行,例如 id =12 和 pc =0 和 pe=20。
    【解决方案3】:

    select distinct c from(select decode(id,12,0,13,13) c from(select a.id from test1 a,test1 b 其中 a.pc=b.pc 和 b.pe=a.pe 和 a.id=b.id)其中 1=1) 其中 c=13;

    【讨论】:

      【解决方案4】:
      select A, B from(
          select decode(pc,0,91,id,0) A, decode(pe,20,201,202,900,901,902,id,0) B from test1
      ) where A <> B;
      

      【讨论】:

        猜你喜欢
        • 2021-01-21
        • 1970-01-01
        • 2013-08-21
        • 2018-11-12
        • 1970-01-01
        • 1970-01-01
        • 2013-03-26
        • 2015-09-15
        • 2012-09-13
        相关资源
        最近更新 更多