select * from Employee
order by
case left(right(Registration_no, 2), 1)
when '/' then
left(Registration_no, len(Registration_no) - 1) + '0' + right(Registration_no, 1)
else Registration_no
end
好的,我可以得到id string id2
1 a 1
2 b 1
3 a 2
4 c 2
5 d 3
:
SELECT * FROM table WHERE string NOT IN ('a','c')
但是我也不想得到包含具有相同id2的a或c的其余行。 结果:
2 b 1
5 d 3
答案 0 :(得分:0)
具有一个子查询,该子查询返回具有a或c的id2值。进行NOT IN
个id2值。
SELECT * FROM table
WHERE id2 not in (select id2 from table where string IN ('a','c'))
如果id2可以包含空值,请改为使用NOT EXISTS
:
SELECT * FROM table t1
WHERE NOT EXISTS (select 1 from table t2
where t2.string IN ('a','c')
and t1.id2 = t2.id2))
答案 1 :(得分:0)
您可以使用not exists
:
select t.*
from t
where not exists (select 1
from t t2
where t2.id2 = t.id2 and t2.string in ('A', 'C')
);
如果只希望使用id2
值,则可能会发现聚合很方便:
select id2
from t
group by id2
having sum(case when string in ('A', 'C') then 1 else 0 end) = 0;