问题描述
Pl找到以下代码
创建表testi3(empcode varchar(30),payroll_code varchar(30),pprdfrom int, pprdto int)
2.插入值
插入testi3值('1','S0010',90,93)
插入testi3值('1','S0009',93,94)
插入testi3值('1','S0008',94,NULL)
插入testi3值('2','S0008',94,NULL)
插入testi3值('2','S0010',90,93)
插入testi3值('3','S0010',90,93)
3.现在如果我们执行下面我得到输出( 1,2)
从testi3中选择*
其中pprdto为空
4.现在我们执行下面我得到输出(1,2,3)但我只需要3,因为其他1,2在前面的行中为Null,
select * from testi3
其中pprdto不是空的
如此友好地帮助一个逻辑如何得到只有3
Pl find below Code
create table testi3 (empcode varchar(30), payroll_code varchar(30),pprdfrom int,pprdto int)
2. insert values
insert into testi3 values ('1','S0010',90,93)
insert into testi3 values ('1','S0009',93,94)
insert into testi3 values ('1','S0008',94,NULL)
insert into testi3 values ('2','S0008',94,NULL)
insert into testi3 values ('2','S0010',90,93)
insert into testi3 values ('3','S0010',90,93)
3. now if we execute below i get output (1,2)
select * from testi3
where pprdto is null
4. now if we execute below i get output (1,2,3) but i only need 3 since other 1,2 as Null in there previous rows,
select * from testi3
where pprdto is not null
so kindly help with a logic how get only 3
推荐答案
select * from testi3 t1
where not exists
(
select * from testi3 t2 where
pprdto is null and t1.empcode = t2.empcode
)
select * from testi3
where pprdto is not null and emp_code not in (select emp_code from testi3 where pprdto is null)
更简洁的解决方案是在同一个表上使用内连接,而不是使用子查询。
A neater solution would be to use inner join on the same table instead of using a sub-query.
这篇关于我该如何解决这类问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!