在一个表中存在
Srv_cd day Pay
711111 1 1000
712111 2 500
713111 3 400
714111 4 200现在要完成这样的功能,
1. 如果这几条数据都不存在的话,就选不出来
2. 如果都存在就都选出来
3. 712111,713111,714111不存在但是711111存在就把这条选出来
4. 如果712111,713111,714111都存在但是711111不存在就把这条也选出来,day和pay就用1和1000固定赋值
前三个功能没什么,就是第四条怎么实现呢
这句sql怎么写才能实现这样的功能,当然表里还有其他数据,不能影响到其他数据的选出,也就是说只有是711111的时候才有第四种情况加上去
Srv_cd day Pay
711111 1 1000
712111 2 500
713111 3 400
714111 4 200现在要完成这样的功能,
1. 如果这几条数据都不存在的话,就选不出来
2. 如果都存在就都选出来
3. 712111,713111,714111不存在但是711111存在就把这条选出来
4. 如果712111,713111,714111都存在但是711111不存在就把这条也选出来,day和pay就用1和1000固定赋值
前三个功能没什么,就是第四条怎么实现呢
这句sql怎么写才能实现这样的功能,当然表里还有其他数据,不能影响到其他数据的选出,也就是说只有是711111的时候才有第四种情况加上去
select * from t where Srv_cd not in (711111, 712111,713111,714111)
union all
select * from t
where Srv_cd in (711111, 712111,713111,714111)
and (select count(*) from t where Srv_cd in (711111, 712111,713111,714111))=4
union all
select * from t
where Srv_cd =711111
and (select count(*) from t where Srv_cd in (711111, 712111,713111,714111))=1
union all
select 711111, 1, 1000
from dual
where (select count(*) from t where Srv_cd in (712111,713111,714111))=3
and not exists (select 1 from t where Srv_cd=711111)