日期:2014-05-20  浏览次数:20834 次

请教一个sql语句
表A:字段:name,status1,status2,status3;
  name varchar;status1,status2,status3 枚举:0,1.

要取得这样的记录 status1,status2,status3 都为0.

求sql语句

------解决方案--------------------
SELECT * FROM A WHERE status1='0' AND status2='0' AND status3 ='1'
------解决方案--------------------
SQL code

select name,status1,status2,status3
from A
where status1=0 and status2=0 and status3=0

------解决方案--------------------
SELECT * FROM 表 A WHERE status1=0 AND status2=0 AND status3 =0
and NOTEXIISTS(SELECT NAME FROM 表 WHERE status1<>0 OR status2<>0 OR status3 <>0 WHERE NAME=A.NAME)
------解决方案--------------------
SQL code

with tb1 as(
select 'aa' name,0 status1,0 status2,1 status3 from dual union all
select 'aa' name,0 status1,0 status2,0 status3 from dual union all
select 'bb' name,1 status1,0 status2,1 status3 from dual union all
select 'bb' name,0 status1,0 status2,0 status3 from dual union all
select 'cc' name,0 status1,0 status2,0 status3 from dual union all
select 'cc' name,0 status1,0 status2,0 status3 from dual
)
select tt.name,tt.status1,tt.status2,tt.status3
from(
select name,
       (select sum(status1) from tb1 where name=t.name) status1,
       (select sum(status2) from tb1 where name=t.name) status2,
       (select sum(status3) from tb1 where name=t.name) status3
from tb1 t
group by name) tt
where status1=0 and status2=0 and status3=0;

--result
NAME    STATUS1    STATUS2    STATUS3
---- ---------- ---------- ----------
cc            0          0          0

------解决方案--------------------
select * from 表A where a.name not in (select name from 表 where status1<>0 or status2<>0 or status3<>0)
------解决方案--------------------
SQL code

select *
  from test
 where TEST.NAME  not in
  (select t.name
  from TEST t
 where status1!=0 or status2!=0 or status3!=0 );

------解决方案--------------------
探讨
SQL code


select *
from test
where TEST.NAME not in
(select t.name
from TEST t
where status1!=0 or status2!=0 or status3!=0 );

------解决方案--------------------
谁说为空啊 最后能得出CC 0 0 0
CC 0 0 0 好吧
------解决方案--------------------
探讨

引用:

引用:

举个例子:如下记录
name status1,status2,status3
aa 0 0 1
aa 0 0 0
bb 1 0 1
bb 0 0 ……

------解决方案--------------------
select a1.*
from A a1
where status1=0 and status2=0 and status3=0 and not exists
(select 1 from A a2 where a2.name=a1.name and (status1=1 or status2=1 or status3=1));
------解决方案--------------------
这个实现肯定麻烦的 与其在这写 sql不如把表结构设计的合理点吧
------解决方案--------------------
这问题简单嘛,楼主,你看我这个怎么样。
SQL code

select a.name,
       a.status1,
       a.status2,
       a.status3
from a
where a.name not in(select a.name
                    from a
                    where a.status1='1'
                    or a.status2='1'
                    or a.status3='1')