日期:2014-05-17  浏览次数:21020 次

PL/SQL學習
ID  Name  Dept
1   張三   A
2   張三   B
3   李四   A
4   李四   B
在以上的oracle表格中如何選出一下條件的內容:
只有Dept為A且Name為張三的那行不要:

選后表格:
ID  Name   Dept
2   張三    B
3   李四     A
4   李四      B
------解决方案--------------------
select id,name,dept from tab1
minus
select id,name,dept from tab1 t
where t.dept='A' and t.name='张三'
------解决方案--------------------
select id, name, dept
  from tab
 where tab.id not in 
 (select id from tab t where t.name = 'A' and t.dept = '張三')
------解决方案--------------------
select id, name, dept
  from tab
 where  t.name
------解决方案--------------------
t.dep !='張三A'
------解决方案--------------------

--构造测试数据
with test as (
select 1 as id,'张三' as name,'A' as dept from dual 
union all
select 2 as id,'张三' as name,'B' as dept from dual 
union all
select 3 as id,'李四' as name,'A' as dept from dual 
union all
select 4 as id,'李四' as name,'B' as dept from dual 
)
--你需要的查询语句:
select * from test where name!='张三' or dept!='A';

--result:
        ID NAME DEPT
---------- ---- ----
         2 张三 B
         3 李四 A
         4 李四 B

难道不应该这么简单?