56,677
社区成员
发帖
与我相关
我的任务
分享
select *
from tb a, (
select t1.id as id1, t2.id as id2, t3.id as id3, t4.id as id4, t5.id as id5
from tb t1, tb t2, tb t3, tb t4, tb t5
where t1.num + t2.num + t3.num + t4.num + t5.num = 5
)b where a.id in (b.id1, b.id2. b.id3, b.id4, b.id5)
select *
from tb a,(
select distinct -- 某些组合达到 num 和 = 5 所需要的 id 一样,所以这里 DISTINCT 一下
case when num1 <= 5 then id1 end as id1,
case when num2 <= 5 then id2 end as id2,
case when num3 <= 5 then id3 end as id3,
case when num4 <= 5 then id4 end as id4,
case when num5 <= 5 then id5 end as id5
from(
select t1.id as id1, t2.id as id2, t3.id as id3, t4.id as id4, t5.id as id5,
t1.num as num1,
t1.num + t2.num as num2,
t1.num + t2.num + t3.num as num3,
t1.num + t2.num + t3.num + t4.num as num4,
t1.num + t2.num + t3.num + t4.num + t5.num as num5
from tb t1, tb t2, tb t3, tb t4, tb t5
where t1.id < t2.id and t2.id < t3.id and t3.id < t4.id and t4.id < t5.id
)x where 5 in (num1, num2, num3, num4, num5 )
) b where a.id in (b.id1, b.id2, b.id3, b.id4, b.id5)
select *
from tb a,(
select
case when num1 <= 5 then id1 end as id1,
case when num2 <= 5 then id2 end as id2,
case when num3 <= 5 then id3 end as id3,
case when num4 <= 5 then id4 end as id4,
case when num5 <= 5 then id5 end as id5
from(
select t1.id as id1, t2.id as id2, t3.id as id3, t4.id as id4, t5.id as id5,
t1.num as num1,
t1.num + t2.num as num2,
t1.num + t2.num + t3.num as num3,
t1.num + t2.num + t3.num + t4.num as num4,
t1.num + t2.num + t3.num + t4.num + t5.num as num5
from tb t1, tb t2, tb t3, tb t4, tb t5
where t1.id < t2.id and t2.id < t3.id and t3.id < t4.id and t4.id < t5.id
)x where 5 in (num1, num2, num3, num4, num5 )
) b where a.id in (b.id1, b.id2, b.id3, b.id4, b.id5)