欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

分享一波SQL刑侦科推理题

程序员文章站 2022-06-24 19:49:19
跟个风 推一下~ with t as ( select 'a' as id from dual union all select 'b' as id fr...

跟个风 推一下~

分享一波SQL刑侦科推理题

with t as
(
select 'a' as id from dual union all
select 'b' as id from dual union all
select 'c' as id from dual union all
select 'd' as id from dual 
),
x as(
select 
t1.id id1, 
t2.id id2,
t3.id id3,
t4.id id4,
t5.id id5,
t6.id id6,
t7.id id7,
t8.id id8,
t9.id id9,
t10.id id10,
t1.id||t2.id||t3.id||t4.id||t5.id||t6.id||t7.id||t8.id||t9.id||t10.id id100
from t t1,t t2,t t3,t t4,t t5,t t6,t t7,t t8,t t9,t t10)
select id100 s from x
where id2=case when id5='c' then 'a'
               when id5='d' then 'b'
               when id5='a' then 'c'
               when id5='b' then 'd' end
  and id3=case when id3 not in(id2,id4,id6) then 'a'
               when id6 not in(id3,id4,id2) then 'b'
               when id2 not in(id3,id4,id6) then 'c' 
               when id4 not in(id3,id6,id2) then 'd' end
  and id4=case when id1=id5 then 'a' 
               when id2=id7 then 'b'
               when id1=id9 then 'c'
               when id6=id10 then 'd' end
  and id5=case when instr(id8||id4||id9||id7,id5)=1 then 'a'
               when instr(id8||id4||id9||id7,id5)=2 then 'b'
               when instr(id8||id4||id9||id7,id5)=3 then 'c'
               when instr(id8||id4||id9||id7,id5)=4 then 'd' end
  and id6=case when id8||id8=id2||id4  then 'a'
               when id8||id8=id1||id6  then 'b'
               when id8||id8=id3||id10 then 'c'
               when id8||id8=id5||id9  then 'd' end
  and id7=decode(   
      greatest(length(replace(id100,'a','')),
               length(replace(id100,'b','')),
               length(replace(id100,'c','')),
               length(replace(id100,'d',''))),
               length(replace(id100,'c','')) , 'a',
               length(replace(id100,'b','')) , 'b',
               length(replace(id100,'a','')) , 'c',
               length(replace(id100,'d','')) , 'd' )
     and id8=case when id1||id7  not in('ab','ba','bc','cb','cd','dc') then 'a'
                  when id1||id5  not in('ab','ba','bc','cb','cd','dc') then 'b'
                  when id1||id2  not in('ab','ba','bc','cb','cd','dc') then 'c'
                  when id1||id10 not in('ab','ba','bc','cb','cd','dc') then 'd' end
     and id9=case when decode(id1,id6,1,0)+decode(id6 ,id5,1,0)=1 then 'a'
                  when decode(id1,id6,1,0)+decode(id10,id5,1,0)=1 then 'b'
                  when decode(id1,id6,1,0)+decode(id2 ,id5,1,0)=1 then 'c'
                  when decode(id1,id6,1,0)+decode(id9 ,id5,1,0)=1 then 'd' end
     and id10=decode(greatest(length(replace(id100,'a','')),
               length(replace(id100,'b','')),
               length(replace(id100,'c','')),
               length(replace(id100,'d','')))
               -least(length(replace(id100,'a','')),
               length(replace(id100,'b','')),
               length(replace(id100,'c','')),
               length(replace(id100,'d',''))),3,'a',2,'b',4,'c',1,'d')

答案是---------bcacacdaba-----------

思路很简单,笛卡尔积出所有的结果(4的10次方行数据),然后使用题中的已知内容构造where条件