+ -
当前位置:首页 → 问答吧 → 求一条sql语句。创建的表都有了。现在根据要求来得到输出。

求一条sql语句。创建的表都有了。现在根据要求来得到输出。

时间:2011-12-06

来源:互联网

create table tmp_table(
  id varchar2(10),
  name varchar2(40),
  flag varchar2(1)
)

创建表的语句。数据
insert into tmp_table (ID, NAME, FLAG) values ('1', '小明', '0'); 
insert into tmp_table (ID, NAME, FLAG) values ('2', '小明', '1'); 
insert into tmp_table (ID, NAME, FLAG) values ('4', '小华', '0'); 
insert into tmp_table (ID, NAME, FLAG) values ('5', '小强', '1'); 
insert into tmp_table (ID, NAME, FLAG) values ('6', '小玲', '1'); 
insert into tmp_table (ID, NAME, FLAG) values ('7', '小红', '0'); 
insert into tmp_table (ID, NAME, FLAG) values ('8', '小红', '1'); 
现在想查询出来的数据是
小明 和小红。
因为小明和小红的flag 标记既有‘0’,也有‘1’。求这样一个语句。
其他的人员要么只有‘1’,要么只用‘0’。

作者: zhangliangming_87   发布时间: 2011-12-06

select distinct(a.name) from tmp_table a, tmp_table b
where a.name = b.name
and ((a.flag = 0 and b.flag = 1)
or (a.flag = 1 and b.flag = 0));

作者: yixilan   发布时间: 2011-12-06

SQL code
select distinct a.name from tmp_table a, tmp_table b where a.name=b.name and a.flag<>b.flag

作者: xpingping   发布时间: 2011-12-06